获取Class反射类:(类) | |
---|---|
Class<Student> a = Student.class; | 通过类名.class属性 |
Class a = Class.forName("com.gzx.test.Student"); | 通过类路径获取 |
Student student = new Student(); Class a = student.getClass(); | 通过对象名获取反射类型 |
Class常用方法:(对象) | |
newInstance(); | 根据反射类得到实例对象 |
getAnnotation(); | 得到反射类上的注解对象 |
获取Method类对象:(方法) | |
getDeclaredMethods() | 得到本类中所有的方法。 |
getDeclaredMethod("方法名",参数类型) | 获取本类中指定的方法对象 |
getMethods() | 获取本类以及父辈类中public修饰的方法。 |
getMethod("方法名",参数类型) | 获取本类以及父辈类中指定public修饰的方法。 |
Method类对象方法:(变量) | |
Object r=invoke(Object对象,参数值) | 传入对象参数 |
setAccessible(true) | 设置允许访问私有成员 |
Field类常用方法:(使用) | |
set(Object对象,值) | 为属性赋值 |
getName() | 获取属性名 |
getAnnotation() | 获取属性上的注解对象 |
反射它是框架设计的灵魂。
反射就是类在运行期间,把类中成员抽取为其他类的过程就是反射。
为什么使用反射:
反射是为了解决在运行期,对某个实例一无所知的情况下,如何调用其方法或属性。
获取Class反射类:
第一种: 通过类名.class属性
Class<Student> aClass = Student.class;第二种: 通过类路径获取
Class aClass1 = Class.forName("com.ykq.test.Student");第三种: 通过对象名获取反射类型
Student student = new Student(); Class aClass2 = student.getClass();
Class常用方法:
1.根据反射类得到实例对象 newInstance()
2.得到反射类上的注解对象。getAnnotation()
package com.gzx.test;
public class Tets1 {
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
Class<Aaa> clazz = Aaa.class;
// 通过反射类创建类对象
Aaa aaa = clazz.newInstance();
System.out.println(aaa);
// 获取反射类上注解对象
MyAnnotation annotation = clazz.getAnnotation(MyAnnotation.class);
System.out.println(annotation.value());
}
}
获取Method类对象:
getDeclaredMethods(): 得到本类中所有的方法。
getDeclaredMethod("方法名",参数类型):获取本类中指定的方法对象
getMethods():获取本类以及父辈类中public修饰的方法。
getMethod("方法名",参数类型):获取本类以及父辈类中指定public修饰的方法。
package com.gzx.test;
import java.lang.reflect.Method;
public class Test2 {
public static void main(String[] args) throws NoSuchMethodException {
Class<Aaa> clazz = Aaa.class;
// 得到本类中定义的所有Method对象
Method[] methods = clazz.getDeclaredMethods();
// 获取本类中指定的方法对象
Method anInt = clazz.getDeclaredMethod("anInt", int.class);
System.out.println(anInt);
// 获取本类以及父类中所有public方法对象
Method[] methods2 = clazz.getMethods();
for (Method method : methods2) {
System.out.println(method);
}
// 获取本类以及父类中执行的public方法对象
Method method = clazz.getMethod("anInt", int.class);
System.out.println(method);
}
}
Method类对象方法:
Object r=invoke(Object对象,参数值)
setAccessible()设置允许访问私有成员
package com.gzx.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test3 {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
// 泛型通配符
Class<?> clazz = Aaa.class;
// 获取实例对象
Object o = clazz.newInstance();
// 无参执行
// 通过方法名使用方法
Method anVoid = clazz.getMethod("anVoid");
// 传参
Object invoke = anVoid.invoke(o);
// 输出
System.out.println(invoke);
// 带参执行
// 通过方法名使用方法并指定传参类型
Method anInt = clazz.getMethod("anInt", int.class);
// 指定实例对象并传入参数
Object invoke1 = anInt.invoke(o, 1);
// 输出
System.out.println(invoke1);
}
}
获取Field属性对象:
getDeclaredField("属性名")
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
Class<Student> aClass = Student.class;
Student student = aClass.newInstance();
Field name = aClass.getDeclaredField("name");
System.out.println(name);
}
Field类常用方法
set(Object对象,值):为属性赋值
getName():获取属性名
getAnnotation():获取属性上的注解对象
package com.gzx.test;
import java.io.File;
import java.lang.reflect.Field;
public class Test4 {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
Class<Aaa> clazz = Aaa.class;
// 实例化对象
Aaa aa = clazz.newInstance();
// 通过反射类实例化成员变量
Field bbb = clazz.getDeclaredField("bbb");
// 修改私有变量可访问性,公用属性可忽略
bbb.setAccessible(true);
// 赋值变量
bbb.set(aa, 10);
// 输出
System.out.println(bbb);
Field[] ccc = clazz.getDeclaredFields();
for (Field f : ccc) {
// 输出变量名
System.out.println(f.getName());
// 获取每个属性对象的注解对象
MyAnnotation a = f.getAnnotation(MyAnnotation.class);
System.out.println(a.value());
}
}
}
----------其中多为自我理解进行总结,可能会有错误,仅供参考---------