反射:
反射是指在程序运行期间,能够观察和修改类或者类的对象的属性和行为的特性
动态获取的信息及动态调用对象的方法的功能称为反射机制
通过Class文件对象,使用该文件中的成员变量,构造方法,成员方法。
反射机制:在程序运行中,能够动态的获取类的信息(成员:属性 方法 内部类 构造 修饰符。。。。只要是能被编译的)
获取Class对象的方式:
1.Object类中的getClass()方法
2.数据类型的静态属性Class
3.Class类中的静态方法
Public static Class forName(String className)
常用方法:
clazz.getSuperclass().getSimpleName()
获取当前类对象所继承的父类类名
clazz.getInterfaces();
获取当前类实现的所有接口,该方法返回值为Class[] 数组
Modifier.toString(clazz.getModifiers())
获取当前类的访问修饰符
clazz.getSimpleName()
获取当前类的类名
clazz.getDeclaredFields()
clazz.getDeclaredField("age")
单数复数形式的方法,clazz.getDeclaredField("age")
标识获取单个成员变量
clazz.getDeclaredFields()标识获取所有成员变量返回值为Filed 类型数组
getFiled()获取非私有字段
getDecalredField()操作私有字段
field.setAccessible(true);屏蔽字段的私有属性
clazz.newInstance();与通过new字段创建示例对象一样
getMethod() 获取该类的指定方法,可指定方法名及参数,包含父类
getMethods() 获取该类所有方法,包含父类
getDeclaredMethod() 获取该类的指定方法,可指定方法名及参数,不包含父类
getDeclaredMethods()获取该类所有方法,不包含父类
getParameterTypes() 获取该方法的参数类型
invoke()调用该类中的方法
clazz.getConstructor()获取构造函数
在Java中的任何一个注解都离不开JDK的四个元注解
@interface
声明注解关键字
1.@Target(当前注解的使用范围)
2.@Documented(API的帮助文档)
3.@Retention(注解类型)
4.@inherited(继承)
clazz.getAnnotation()方法返回该元素的指定类型的注解
以下为各种方法示例:
public class Test {
public static void main(String[] args) throws Exception {
Class clazz = Class.forName("ld.test.Student");
getSuperClassAndInterfaces(clazz);
System.out.println("\n===============================");
getClassAndModifiers(clazz);
System.out.println("\n===============================");
getFields(clazz);
System.out.println("\n===============================");
getMethods(clazz);
System.out.println("\n===============================");
getConstructors(clazz);
System.out.println("\n===============================");
getAnnotation(clazz);
}
// 1: 获取指定类继承的父类包括实现的接口
public static void getSuperClassAndInterfaces(Class clazz) {
System.out.println("该类实现的父类是:" + clazz.getSuperclass().getSimpleName());
System.out.println("该类实现接口有:");
Class[] interfaces = clazz.getInterfaces();
for (Class class1 : interfaces) {
System.out.print(class1.getSimpleName() + ",");
}
}
// 2.获取类名+访问修饰符
public static void getClassAndModifiers(Class clazz) {
System.out.print(Modifier.toString(clazz.getModifiers()));
System.out.print(" class");
System.out.print(" " + clazz.getSimpleName());
}
// 3.获取所有成员变量
public static void getFields(Class clazz) throws Exception {
// 单数和复数形式的方法 getField getDeclaredField(单个) 加s获取所有
// getField获取非私有字段 getDecalredField操作私有
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
System.out.println(field.getModifiers() + " "
+ field.getType().getSimpleName() + " " + field.getName());
}
Field field = clazz.getDeclaredField("age");
// 与通过new关键字创建的实例对象一样
Object obj = clazz.newInstance();
// 屏蔽字段的私有属性
field.setAccessible(true);
field.set(obj, 16);
Student stu = (Student) obj;
System.out.println(stu.getAge());
}
// 4.获取所有成员方法
public static void getMethods(Class clazz) throws Exception {
Method[] methods = clazz.getMethods();
Method[] declaredMethods = clazz.getDeclaredMethods();
System.out.println("该类所有方法,包含父类:");
for (Method method : methods) {
System.out.println(Modifier.toString(method.getModifiers()) + " "
+ method.getReturnType().getSimpleName() + " "
+ method.getName() + "方法参数有:");
for (Class classes : method.getParameterTypes()) {
System.out.print(classes.getSimpleName() + ",");
}
}
System.out.println("该类所有方法,不包含父类:");
for (Method method : declaredMethods) {
System.out.println(Modifier.toString(method.getModifiers()) + " "
+ method.getReturnType().getSimpleName() + " "
+ method.getName());
System.out.println(method.getName() + "方法的参数有:");
for (Class classes : method.getParameterTypes()) {
System.out.print(classes.getSimpleName() + ",");
}
}
Method method = clazz.getDeclaredMethod("show");
Object obj = clazz.newInstance();
method.invoke(obj);
}
//5.获取构造函数
public static void getConstructors(Class clazz) throws Exception{
Constructor constructor = clazz.getConstructor(String.class,int.class,String.class);
Object obj = constructor.newInstance("小明",17,"8888");
Student stu =(Student)obj;
stu.show();
}
//6.注解
public static void getAnnotation(Class clazz) throws Exception{
StudentAnnotation annotation = (StudentAnnotation)clazz.getAnnotation(StudentAnnotation.class);
System.out.println("学生名称:"+annotation.studentName()+",学号:"+annotation.studentNo());
StudentAnnotation annotation2 = clazz.getDeclaredField("name").getAnnotation(StudentAnnotation.class);
System.out.println("学生名称:"+annotation2.studentName()+",学号:"+annotation2.studentNo());
StudentAnnotation annotation3 = clazz.getDeclaredMethod("getName").getAnnotation(StudentAnnotation.class);
System.out.println("学生名称:"+annotation3.studentName()+",学号:"+annotation3.studentNo());
}
}