一篇java中的反射
1.Class对象
每当编写并且编译了一个新类,就会产生一个Class对象
Class类和java.lang.reflect类库一起对反射进行了支持
1.Class方法
- object instanceof class(这个class代指类的名字,不是常说的java class类)
- public native boolean isInstance(Object obj);//father.class.isInstance(son)
- public native boolean isAssignableFrom(Class<?> cls); //father.class.isAssgnableFrom(son.class)
1、2、3的结果相同,保留了某个对象是这个类么,或者是这个类的派生类么(子孙)
而equals和==的结果相同,没有考虑继承,它比较的是这个对象的确切类型
2.反射中常用到的方法
class | Description |
---|---|
Constructor | Constructor provides information about, and access to, a single constructor for a class. |
Field | A Field provides information about, and dynamic access to, a single field of a class or an interface. |
Method | A Method provides information about, and access to, a single method on a class or interface. |
Modifier | The Modifier class provides static methods and constants to decode class and member access modifiers. |
Parameter | Information about method parameters. |
Proxy | Proxy provides static methods for creating objects that act like instances of interfaces but allow for customized method invocation. |