反射
- 反射就是:加载类,并允许以编程的方式解剖类中的各种成分(成员变量、方法、构造器等)。
反射学什么?
学习获取类的信息、操作它们
- 反射第一步:加载类,获取类的字节码:Class对象
- 获取类的构造器:Constructor对象
- 获取类的成员变量:Field对象
- 获取类的成员方法:Method对象
获取Class对象的三种方式
- Class c1 =类名.class
- 调用Class提供方法:public static Class forName(String package);
- Object提供的方法:public Class getClass(); Class c3=对象.getclass();
代码演示如下:
public class Test1Class { public static void main(String[] args) throws Exception{ Class c1 = Student.class; System.out.println(c1.getName()); // 全类名 System.out.println(c1.getSimpleName()); // 简名:Student Class c2 = Class.forName("com.birdbird.d2_reflect.Student"); System.out.println(c1 == c2); Student s = new Student(); Class c3 = s.getClass(); System.out.println(c3 == c2); } }
代码演示如下:
public class Cat { private String name; private int age; public Cat() { } public Cat(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
测试类:
public class Test2Constructor { @Test public void testGetConstructors(){ //1.反射第一步:必须先得到这个类的Class对象 Class c = Cat.class; //2.获取类的全部构造器 //Constructor[] constructors = c.getConstructors(); Constructor[] constructors = c.getDeclaredConstructors(); //3.遍历数组中的每个构造器对象 for (Constructor constructor : constructors) { System.out.println(constructor.getName() + "--->" + constructor.getParameterCount()); } } @Test public void testGetConstructor() throws Exception{ //1.反射第一步:必须先得到这个类的Class对象 Class c = Cat.class; //2.获取类的某个构造器,无参数构造器 Constructor constructor1 = c.getConstructor(); System.out.println(constructor1.getName() + "--->" + constructor1.getParameterCount()); //3.获取有参数构造器 Constructor constructor2 = c.getDeclaredConstructor(String.class, int.class); System.out.println(constructor2.getName() + "--->" + constructor2.getParameterCount()); } }
代码演示如下:
public class Cat { private String name; private int age; private Cat() { System.out.println("无参构造器执行了~"); } public Cat(String name, int age) { System.out.println("有参数构造器执行了~"); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class Test2Constructor { @Test public void testGetConstructors(){ //1.反射第一步:必须先得到这个类的Class对象 Class c = Cat.class; //2.获取类的全部构造器 //Constructor[] constructors = c.getConstructors(); Constructor[] constructors = c.getDeclaredConstructors(); //3.遍历数组中的每个构造器对象 for (Constructor constructor : constructors) { System.out.println(constructor.getName() + "--->" + constructor.getParameterCount()); } } @Test public void testGetConstructor() throws Exception{ //1.反射第一步:必须先得到这个类的Class对象 Class c = Cat.class; //2.获取类的某个构造器,无参数构造器 Constructor constructor1 = c.getDeclaredConstructor(); System.out.println(constructor1.getName() + "--->" + constructor1.getParameterCount()); constructor1.setAccessible(true); // 禁止检查访问权限 Cat cat = (Cat) constructor1.newInstance(); System.out.println(cat); //3.获取有参数构造器 Constructor constructor2 = c.getDeclaredConstructor(String.class, int.class); System.out.println(constructor2.getName() + "--->" + constructor2.getParameterCount()); constructor2.setAccessible(true); // 禁止检查访问权限 Cat cat2 = (Cat) constructor2.newInstance("机器猫",3); System.out.println(cat2); } }