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这个类中有一些方法可以获得Constructor、Constructor[]、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.
- Class类与 Reflect Api
- Class类与映射API
- 黑马程序员----Class类、反射(Reflect)
- Object类与reflect包
- 黑马程序员——Class类、反射(Reflect)
- class sun.reflect.NativeMethodAccessorImpl请问这个类在哪里啊
- 【JVM】Unloading Class Class.Sun.Reflect.GeneratedMethodAccessor141
- 笔记--反射(Reflect)基石之Class类--2011/9/5 10:17
- 黑马程序员_高新技术二(Class类,反射Reflect(重点知识))
- 黑马程序员_高新技术2(Class类,反射Reflect(重点知识))
- 黑马程序员--Java基础加强学习笔记之Class类、反射(Reflect)
- Java--Reflect(反射)专题1——class 类的使用
- JAVA学习之反射(1):Class类和java.lang.reflect包的使用
- 反射 java.lang.Class java.lang.reflect.*
- 关于 "Unloading class sun.reflect.GeneratedSerial...
- 关于 "Unloading class sun.reflect.GeneratedSerial...
- 关于 "Unloading class sun.reflect.GeneratedSerial...
- Class API
- java异常
- os2ip及i2osp的实现
- 【转】Bill Gates' 11 Rules
- 浅析:setsockopt()改善程序的健壮性
- 世界头号电脑黑客 - 凯文•米特尼克(Kevin Mitnick)
- Class类与 Reflect Api
- [转]程序人生-我已经努力了七年
- 汇编强化训练
- C语言文件操作之----文件的读写
- 在桌面中嵌入窗体
- 实现海量数据和小数据量的通用分页显示存储过程
- 操作系统之--银行家算法1
- HOOK专题
- Lest We Remember: Cold Boot Attacks on Encryption Keys