反射
1. 类对象
万物皆对象,我们创建的类本身也是对象,成为类对象,属于引用类型(Class)的对象
2. 获取方式
- 通过对象获取
- 通过类名获取
- 通过类路径获取
package com.qfedu;
public class Demo01 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Student stu = new Student();
/*
* 我们创建的类,本身也是一个对象,叫做类对象
*/
Class class1 = stu.getClass();
Class class2 = Student.class;
String path = "com.qfedu.Student";
Class class3 = Class.forName(path);
System.out.println(class3);
Student stu2 = (Student) class3.newInstance();
}
}
class Student {
String name;
Integer age;
}
3. 类对象操作
- 获取类信息,类名,包名,父类,实现的接口
- 获取属性,构造方法,实例方法
package com.qfedu;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Demo02 {
public static void main(String[] args) throws ClassNotFoundException {
String path = "com.qfedu.Computer";
Class clazz = Class.forName(path);
/*
* 类对象的操作
* 1. 获取类的全称
* 2. 获取包路径
* 3. 获取父类
* 4. 获取 属性,构造方法,实例方法
*/
String name = clazz.getName();
System.out.println(name);
Package page = clazz.getPackage();
System.out.println(page);
Class superclass = clazz.getSuperclass();
System.out.println(superclass);
/*
* 获取属性,每个属性在java都是Field对象
* getFields() : 获取公共属性
* getDeclaredFields() : 获取所有方法
*
*/
Field[] fields = clazz.getDeclaredFields();
for(Field field : fields) {
System.out.println(field);
}
/*
* 获取方法,每个方法也是Method对象
* getMethods() : 获取公共方法
* getDeclaredMethods() : 获取所有方法
*/
Method[] methods = clazz.getDeclaredMethods();
for(Method method : methods) {
System.out.println(method);
}
/*
* 获取构造方法,每个构造方法也是对象
* getConstructors() : 获取公共构造方法
* getDeclaredConstructors() : 获取所有构造方法
*/
Constructor[] constructors = clazz.getDeclaredConstructors();
for(Constructor constructor : constructors) {
System.out.println(constructor);
}
}
}
class Computer {
private String brand;
public Double price;
String color;
public Computer() {
}
public Computer(String brand, Double price, String color) {
super();
this.brand = brand;
this.price = price;
this.color = color;
}
public void playGame() {
System.out.println("玩游戏");
}
public void code() {
System.out.println("写代码");
}
}
4. 构造方法对象
- 每一个构造方法都是一个Constructor对象
- 可以通过该对象创建一个该类的对象
package com.qfedu;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Demo03 {
public static void main(String[] args) throws Exception {
//获取Person的类对象
String path = "com.qfedu.Person";
Class clazz = Class.forName(path);
//得到无参构造方法
Constructor constructors = clazz.getConstructor();
Constructor constructors2 =
clazz.getConstructor(String.class, Integer.class, String.class);
Person p1 = (Person) constructors.newInstance();
Person p2 = (Person) constructors2.newInstance("jackma",18, "男");
System.out.println(p2);
}
}
5. 属性对象
- 每一个属性都是一个Field对象
- 可以通过set方法为该属性赋值
- 可以通过get方法获取该属性值
package com.qfedu;
import java.lang.reflect.Field;
public class Demo04 {
public static void main(String[] args) throws Exception {
//获取Person的类对象
String path = "com.qfedu.Person";
Class clazz = Class.forName(path);
//获取一个属性
Field field = clazz.getDeclaredField("name");
//由于属性是私有,在操作之前,要手动赋权
field.setAccessible(true);
Object obj = clazz.newInstance();
//为一个对象的该属性赋值
field.set(obj, "pony");
//获取给对象的值
String name = (String) field.get(obj);
System.out.println(name);
}
}
6. 方法对象
- 每一个实例方法都是一个Method对象
- 可以通过调用该对象的invoke方法调用该方法
package com.qfedu;
import java.lang.reflect.Method;
public class Demo05 {
public static void main(String[] args) throws Exception {
//获取Person的类对象
String path = "com.qfedu.Person";
Class<?> clazz = Class.forName(path);
Method method = clazz.getMethod("setName",String.class);
Object obj = clazz.newInstance();
method.invoke(obj, "jackma");
Method method2 = clazz.getMethod("getName");
String name = (String) method2.invoke(obj);
System.out.println(name);
}
}