Class类与 Reflect Api

来源:互联网 发布:淘宝网上买白酒 编辑:程序博客网 时间:2024/06/14 23:34

Class类与 Reflect Api

 

Java中,每个class都有一个相应的Class对象。也就是说,当我们编写一个类,编译完成后,在生成的.class文件中,就会产生一个Class对象,用于表示这个类的类型信息。

在运行期间,如果我们要产生某个类的对象,Java虚拟机(JVM)会检查该类型的Class对象是否已被加载。如果没有被加载,JVM会根据类的名称找到.class文件并加载它。一旦某个类型的Class对象已被加载到内存,就可以用它来产生该类型的所有对象。

对于上面这段话,我们要深刻的去理解,实际上是告诉我们类的加载过程,实际上产生一个对象或者,访问这个类的静态成员时都会去加载类,加载的方法就是向上面那样。我们有什么办法获得一个Class实例呢?一共有三种方法如下:

1)利用对象调用getClass()方法获取该对象的Class实例;

2)使用Class类的静态方法forName(),用类的名字获取一个Class实例;在动态的加载类的时候,还是很有用的,比如我们要加载一个数据苦的驱动程序,一般就要用到这个方法,当然这个方法要抛出异常,应该捕获或者声明为抛出。

3)运用.class的方式来获取Class实例,对于基本数据类型的封装类,还可以采用.TYPE来获取相对应的基本数据类型的Class实例。(int.class ,Integre.class ,Integer.TYPE

newInstance()Clas类中的一个方法, 它能够调用类中缺省的构造方法,但用要注意只能是不带参数的构造方法,那我们当然可以利用命令行的传递方式,或者从网上动态的加载一个类(firName()),并获得一个类的对象,但我们只能利用 newInstannce()来调用我们动态加载类中的没有参数的构造方法,似乎有些用处不大,这个时候我们可以利用反射API(java.lang.reflect包中)来解决这个问题。关于反射API 的说明如下(摘自tutorial

The reflection API represents, or reflects, the classes, interfaces, and objects in the current Java Virtual Machine. You'll want to use the reflection API if you are writing development tools such as debuggers, class browsers, and GUI builders. With the reflection API you can:

  • Determine the class of an object. 可以动态产生一个类的对象
  • Get information about a class's modifiers, fields, methods, constructors, and superclasses. 可以得到这个类的修饰符、字段、方法、构造器和父类的信息。
  • Find out what constants and method declarations belong to an interface. 找出一个接口定义的常量和方法。
  • Create an instance of a class whose name is not known until runtime.
  • Get and set the value of an object's field, even if the field name is unknown to your program until runtime.
  • Invoke a method on an object, even if the method is not known until runtime.
  • Create a new array, whose size and component type are not known until runtime, and then modify the array's components.

Class这个类中有一些方法可以获得ConstructorConstructor[]Method Method[]。这样我们就可以利用这些获得到的对象调用其中的方法完成一些功能,比如Constructor对象的newInstance(Object [] initArgus)方法就获得类的实例,但是需要一个参数信息,具体这个参数信息的获得还要借助Constructor对象的另一个方法Class[] getParameterTypes()它用来返回构造器的所有参数类型。要调用类中别的方法,我们就要用到 Method这个类的invoke(Object o, Object... args)方法        {  Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.}.

reflection API虽然很强大可以模拟函数指针的功能但是我们要慎重的去使用。在指南中这样写:First, a note of caution. Don't use the reflection API when other tools more natural to the Java programming language would suffice. For example, if you are in the habit of using function pointers in another language, you might be tempted to use the Method objects of the reflection API in the same way. Resist the temptation! Your program will be easier to debug and maintain if you don't use Method objects. Instead, you should define an interface, and then implement it in the classes that perform the needed action.

 

 
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 6个月宝宝断奶哭闹不用奶瓶怎么办 小孩不爱吃饭怎么办该吃些什么 二个月宝宝只认母乳不喝牛奶怎么办 满月宝宝只认奶粉不认母乳怎么办 宝宝四个月只认奶瓶不认母乳怎么办 新生儿只认奶瓶不认母乳怎么办 7个月宝宝不吃辅食怎么办 8个月小孩不吃辅食怎么办 9个月小孩不吃辅食怎么办 十一个月宝宝拉肚子拉水怎么办 小孩好动上课也坐不住会怎么办 幼儿大班关于环保的手抄报怎么办 捷达车打开丝位子风扇总转怎么办 小班个案分析:好动的孩子该怎么办 孩子突然过于的兴奋不睡觉是怎么办 刚出生的宝宝听力未通过怎么办 孩子多动症被老师打不上学怎么办 小孩在校被学生打家长该怎么办? 8岁儿童好动上课不注意听课怎么办 怀孕第一个月喝了啤酒怎么办 不知道自己怀孕喝了酒怎么办 怀孕一个月不知道喝酒了怎么办 老师说孩子上课不专心听讲怎么办 二年级孩子上课不认真听讲怎么办 二年级的孩子浮躁上课不认真怎么办 三年级的小孩上课不认真听讲怎么办 大班孩子上课不专心听讲玩怎么办 老师跟家长说小孩上课讲话怎么办 别的家长动手打自己的孩子怎么办? 一岁3个月宝宝骨龄偏小怎么办 宝宝9个月了越来越粘人怎么办? 孩子突然说话结巴口吃了怎么办啊 很久不说话了不敢说话了怎么办 宝宝2岁了还不会说话怎么办 2岁的宝宝还不会说话怎么办 小宝宝有四个月了母乳不够吃怎么办 一个月的宝宝吐奶厉害怎么办 新生儿吐奶吐一次吐的特别多怎么办 把孩子打了一次现在说话结巴怎么办 宝宝五岁了口吃越来越严重了怎么办 幼儿把自己的舌头扣破皮了怎么办