Class类与 Reflect Api

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.

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值