通俗地说,反射机制就是可以把一个类,类的成员(函数,属性),当成一个对象来操作,希望读者能理解,也就是说,类,类的成员,我们在运行的时候还可以动态地去操作他们.
理论的东东太多也没用,下面我们看看实践 Demo ~
Demo:
package cn.lee.demo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.TypeVariable;
public class Main {
/**
* 为了看清楚Java反射部分代码,所有异常我都最后抛出来给虚拟机处理!
* @param args
* @throws ClassNotFoundException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws NoSuchMethodException
*/
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchFieldException, NoSuchMethodException {
// TODO Auto-generated method stub
//Demo1. 通过Java反射机制得到类的包名和类名
Demo1();
System.out.println("===============================================");
//Demo2. 验证所有的类都是Class类的实例对象
Demo2();
System.out.println("===============================================");
//Demo3. 通过Java反射机制,用Class 创建类对象[这也就是反射存在的意义所在],无参构造
Demo3();
System.out.println("===============================================");
//Demo4: 通过Java反射机制得到一个类的构造函数,并实现构造带参实例对象
Demo4();
System.out.println("===============================================");
//Demo5: 通过Java反射机制操作成员变量, set 和 get
Demo5();
System.out.println("===============================================");
//Demo6: 通过Java反射机制得到类的一些属性: 继承的接口,父类,函数信息,成员信息,类型等
Demo6();
System.out.println("===============================================");
//Demo7: 通过Java反射机制调用类中方法
Demo7();
System.out.println("===============================================");
//Demo8: 通过Java反射机制获得类加载器
Demo8();
System.out.println("===============================================");
}
/**
* Demo1: 通过Java反射机制得到类的包名和类名
*/
public static void Demo1()
{
Person person = new Person();
System.out.println("Demo1: 包名: " + person.getClass().getPackage().getName() + ","
+ "完整类名: " + person.getClass().getName());
}
/**
* Demo2: 验证所有的类都是Class类的实例对象
* @throws ClassNotFoundException
*/
public static void Demo2() throws ClassNotFoundException
{
//定义两个类型都未知的Class , 设置初值为null, 看看如何给它们赋值成Person类
Class<?> class1 = null;
Class<?> class2 = null;
//写法1, 可能抛出 ClassNotFoundException [多用这个写法]
class1 = Class.forName("cn.lee.demo.Person");
System.out.println("Demo2:(写法1) 包名: " + class1.getPackage().getName() + ","
+ "完整类名: " + class1.getName());
//写法2
class2 = Person.class;
System.out.println("Demo2:(写法2) 包名: " + class2.getPackage().getName() + ","
+ "完整类名: " + class2.getName());
}
/**
* Demo3: 通过Java反射机制,用Class 创建类对象[这也就是反射存在的意义所在]
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void Demo3() throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
Class<?> class1 = null;
class1 = Class.forName("cn.lee.demo.Person");
//由于这里不能带参数,所以你要实例化的这个类Person,一定要有无参构造函数哈~
Person person = (Person) class1.newInstance();
person.setAge(20);
person.setName("LeeFeng");
System.out.println("Demo3: " + person.getName() + " : " + person.getAge());
}
/**
* Demo4: 通过Java反射机制得到一个类的构造函数,并实现创建带参实例对象
* @throws ClassNotFoundException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws IllegalArgumentException
*/
public static void Demo4() throws ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException
{
Class<?> class1 = null;
Person person1 = null;
Person person2 = null;
class1 = Class.forName("cn.lee.demo.Person");
//得到一系列构造函数集合
Constructor<?>[] constructors = class1.getConstructors();
person1 = (Person) constructors[0].newInstance();
person1.setAge(30);
person1.setName("leeFeng");
person2 = (Person) constructors[1].newInstance(20,"leeFeng");
System.out.println("Demo4: " + person1.getName() + " : " + person1.getAge()
+ " , " + person2.getName() + " : " + person2.getAge()
);
}
/**
* Demo5: 通过Java反射机制操作成员变量, set 和 get
*
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
public static void Demo5() throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException, InstantiationException, ClassNotFoundException
{
Class<?> class1 = null;
class1 = Class.forName("cn.lee.demo.Person");
Object obj = class1.newInstance();
Field personNameField = class1.getDeclaredField("name");
personNameField.setAccessible(true);
personNameField.set(obj, "胖虎先森");
System.out.println("Demo5: 修改属性之后得到属性变量的值:" + personNameField.get(obj));
}