一,反射的概念
1,什么是反射?
反射(Reflection):允许程序在运行时查询,调用类的所有信息。
2,反射的作用
获取类的包
获取类的注解
获取类的接口
获取类的父类
获取类的属性并调用
获取类的构造方法并调用
获取类的方法并调用
获取类的对象并调用
二,Class类
1,概述
描述类的类,反射的根源
2,获得Class对象的方式
类.class
对象.getClass()
Class.forName()
代码:
public class ClassTest {
public static void main(String[] args) throws Exception {
System.out.println("----使用类.属性来获取该类的Class对象---");
Class<Student> s1 = Student.class;
System.out.println(s1);
Class<Student> s2 = Student.class;
System.out.println(s2);
System.out.println(s1==s2);
System.out.println("----使用对象.getClass()方法,返回该对象所属类对应的Class对象---");
Student student = new Student();
Class<? extends Student> s3 = student.getClass();
System.out.println(s1==s3);
System.out.println("--使用Class.forName(全类名)来获取该类对应的Class对象---");
Class<?> s4 = Class.forName("com.hg.pojo.Student");
System.out.println(s1==s4);
}
}
三,Constructor类
1,概述
描述构造方法的类
2,获得Constructor对象的方式
Constructor<?>[] getConstructors() : 返回所有的公共构造方法
Constructor getConstructor(class<?>... parameterTypes): 返回单个公共构造方法
3,常用方法
T newInstance(Object... initargs):创建对象
代码:
public class ConstructorTest {
public static void main(String[] args) throws Exception {
Class<?> c1 = Class.forName("com.hg.pojo.Student");
// System.out.println("----返回所有的构造方法的数组----");
// Constructor<?>[] declaredConstructors = c1.getDeclaredConstructors();
// for (Constructor<?> declaredConstructor : declaredConstructors) {
// System.out.println(declaredConstructor);
// }
//
// System.out.println("----返回单个构造方法----");
// Constructor<?> constructor = c1.getConstructor();
// System.out.println(constructor);
//
// Constructor<?> constructor1 = c1.getConstructor(String.class, Integer.class, String.class);
// System.out.println(constructor1);
// 第一种
Constructor<?> constructor = c1.getConstructor();
Object obj = constructor.newInstance();
System.out.println(obj);
// 第二种
Constructor<?> constructor1 = c1.getConstructor(String.class, Integer.class, String.class);
Object obj1 = constructor1.newInstance("林青霞",18,"西安");
System.out.println(obj1);
// 第三种
Constructor<?> constructor2 = c1.getConstructor(String.class, Integer.class);
Object obj2 = constructor2.newInstance("林青霞",18);
System.out.println(obj2);
}
}
四,Field类
1,概述
描述属性的类
2,获得Field对象的方式
Field[] getDeclaredFields():返回所有属性
Filed[] getDeclaredField(String name):返回单个属性
3,常用方法
void set (Object obj,Object value):赋值
代码:
public class FieldTest {
public static void main(String[] args) throws Exception {
Student student = new Student();
Class<? extends Student> aClass = student.getClass();
System.out.println("--通过Field[] getDeclaredFields()获取所有属性--");
Field[] declaredFields = aClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
System.out.println("--通过Field getDeclaredField(String name)获取单个属性--");
Field name = aClass.getDeclaredField("name");
System.out.println(name);
Field age = aClass.getDeclaredField("age");
System.out.println(age);
Field address = aClass.getDeclaredField("address");
System.out.println(address);
System.out.println("--void set(object obj,Object value)--");
Student student1 = aClass.getConstructor().newInstance();
name.setAccessible(true); // 开启私有属性的访问
age.setAccessible(true); // 开启私有属性的访问
address.setAccessible(true);
name.set(student1,"郭襄");
age.set(student1,18);
address.set(student1,"峨眉山");
System.out.println(student1);
}
}
五,Method类
1,概述
描述方法的类
2,获取Method对象的方式
Method[] getMethods():获得所有的公共方法
Method getMethod(String name,Class<?>...parameterTypes):获得单个公共方法
3,常用方法
Object invoke(Object obj,Object...args):调用方法
代码:
public class MethodTest {
public static void main(String[] args) throws Exception {
Class<?> aClass = Class.forName("com.hg.pojo.Student");
System.out.println("---通过Method[] getMethods获得所有的公共方法---");
Method[] methods = aClass.getMethods();
// for (Method method : methods) {
// System.out.println(method);
// }
System.out.println("--通过Method getMethod(String name,class<?>...parameterTypes)获得单个公共方法--");
Method method1 = aClass.getMethod("method1");
System.out.println(method1);
Method method2 = aClass.getMethod("method2", String.class);
System.out.println(method2);
Method method3 = aClass.getMethod("method3", String.class, Integer.class);
System.out.println(method3);
System.out.println("--通过Object invoke(Object obj,Object...args)调用方法--");
Student student = (Student) aClass.getConstructor().newInstance();
method1.invoke(student);
method2.invoke(student,"殷素素");
Object dudu = method3.invoke(student, "嘟嘟", 18);
System.out.println(dudu);
}
}
五,Annotation
1,概述
描述注解的类
2,获得Annotation对象的方式
Annotation[] getAnnotations():获得所有的注解
Annotation getAnnotation(Class<?>...annotationClass):获得单个注解
代码:
public class AnnotationTest {
public static void main(String[] args) throws Exception {
Class<?> aClass = Class.forName("com.hg.pojo.CustomClass");
System.out.println("--通过Annotation[] getAnnotation获得类上的所有注解--");
Annotation[] annotations = aClass.getAnnotations();
for (Annotation annotation : annotations) {
CustomAnnotation customAnnotation = (CustomAnnotation) annotation;
System.out.println(customAnnotation.name()+"\t"+customAnnotation.value());
}
System.out.println("--通过Annotation[] getAnnotation() 获取属性上的所有注解--");
Annotation[] customFields = aClass.getDeclaredField("customField").getAnnotations();
for (Annotation customField : customFields) {
CustomAnnotation customAnnotation = (CustomAnnotation) customField;
System.out.println(customAnnotation.name()+"\t"+customAnnotation.value());
}
System.out.println("--通过Annotation[] getAnnotations() 获取方法上的所有注解--");
Annotation[] customMethods = aClass.getMethod("customMethod").getAnnotations();
for (Annotation customMethod : customMethods) {
CustomAnnotation customAnnotation = (CustomAnnotation) customMethod;
System.out.println(customAnnotation.name()+"\t"+customAnnotation.value());
}
System.out.println("--通过Annotation getAnnotation(class<?>... annotationClass)获取类上的单个注解--");
CustomAnnotation annotation = aClass.getAnnotation(CustomAnnotation.class);
System.out.println(annotation.name()+"\t"+annotation.value());
System.out.println("--通过Annotation getAnnotation(Class<?>... annotationClass)获取属性上的单个注解--");
CustomAnnotation customField = aClass.getDeclaredField("customField").getAnnotation(CustomAnnotation.class);
System.out.println(customField.name()+"\t"+customField.value());
System.out.println("--通过Annotation getAnnotation(Class<?>... annotationClass)获取方法上的单个注解--");
CustomAnnotation customMethod = aClass.getMethod("customMethod").getAnnotation(CustomAnnotation.class);
System.out.println(customMethod.name()+"\t"+customMethod.value());
}
}