Java 反射
一、基本信息读取,构造方法、属性(属性类型、属性修饰符),内部方法(返回值、修饰符)
1、Constructor
getConstructors:public com.example.reflection.Girls(java.lang.String , int , int , int , java.lang.String)
getDeclaredConstructors:
2、Fields
getFields:获取被声明为public的属性字段 例如:public int age
getDeclaredFields:获取到所有字段,public int age , private class java.lang.String name;
3、Methods
getMethods:获取到父类及父类的父类的所有方法,还有native方法
getDeclaredMethods:获取到本类的方法,包括复写的父类方法
二、获知父类与实现的接口
1、获取父类
getSuperclass 与 getGenericSuperclass
1、如果该Class是接口,两个方法均返回null
2、getSuperclass 返回父类完整名称 如 class com.example.reflection.ParentClass
3、getGenericSuperclass ,根据父类,存在两种可能情况:
1)比如默认继承Object , 返回 class java.lang.Object,
2)如果父类是 ParentClass<T>,这样情况下 com.example.reflection.ParentClass<java.lang.String>
2、获取实现的接口,类似于获取继承的父类
getInterfaces 与 getGenericInterfaces
1、两个方法返回的是 clz 实现的所有接口,即接口数组
2、getInterfaces,返回的是接口完整名称,interface com.example.reflection.ParameterInterface 、interface com.example.reflection.ReflectInterface
3、getGenericInterfaces ,同 getGenericSuperclass ,
com.example.reflection.ParameterInterface<java.lang.Integer> 、 interface com.example.reflection.ReflectInterface
三、方法调用
1、 Class<?> clz = Class.forName("className");
Method methodName = clz.getDeclaredMethod("methodName",parameters_type);// parameters_type 是 可变长度的Object数组 ,即该方法所有参数类型
method.setAccessible(true); // 如果methodName 修饰符限定 无法访问情况下,调用该方法设置true ,使突破限制,外界可以访问
Object object = method.invoke(clz); // 如果对应methodName有返回值 object 即为对应返回值 否则为null
2、Object o = clz.newInstance(); // 如果对应Class没有无参构造方法 或者 是 普通内部类 会报错
将 o 强转成对应类型对象,即可作为普通类对象使用,包括方法调用
一、基本信息读取,构造方法、属性(属性类型、属性修饰符),内部方法(返回值、修饰符)
1、Constructor
getConstructors:public com.example.reflection.Girls(java.lang.String , int , int , int , java.lang.String)
getDeclaredConstructors:
2、Fields
getFields:获取被声明为public的属性字段 例如:public int age
getDeclaredFields:获取到所有字段,public int age , private class java.lang.String name;
3、Methods
getMethods:获取到父类及父类的父类的所有方法,还有native方法
getDeclaredMethods:获取到本类的方法,包括复写的父类方法
二、获知父类与实现的接口
1、获取父类
getSuperclass 与 getGenericSuperclass
1、如果该Class是接口,两个方法均返回null
2、getSuperclass 返回父类完整名称 如 class com.example.reflection.ParentClass
3、getGenericSuperclass ,根据父类,存在两种可能情况:
1)比如默认继承Object , 返回 class java.lang.Object,
2)如果父类是 ParentClass<T>,这样情况下 com.example.reflection.ParentClass<java.lang.String>
2、获取实现的接口,类似于获取继承的父类
getInterfaces 与 getGenericInterfaces
1、两个方法返回的是 clz 实现的所有接口,即接口数组
2、getInterfaces,返回的是接口完整名称,interface com.example.reflection.ParameterInterface 、interface com.example.reflection.ReflectInterface
3、getGenericInterfaces ,同 getGenericSuperclass ,
com.example.reflection.ParameterInterface<java.lang.Integer> 、 interface com.example.reflection.ReflectInterface
三、方法调用
1、 Class<?> clz = Class.forName("className");
Method methodName = clz.getDeclaredMethod("methodName",parameters_type);// parameters_type 是 可变长度的Object数组 ,即该方法所有参数类型
method.setAccessible(true); // 如果methodName 修饰符限定 无法访问情况下,调用该方法设置true ,使突破限制,外界可以访问
Object object = method.invoke(clz); // 如果对应methodName有返回值 object 即为对应返回值 否则为null
2、Object o = clz.newInstance(); // 如果对应Class没有无参构造方法 或者 是 普通内部类 会报错
将 o 强转成对应类型对象,即可作为普通类对象使用,包括方法调用