JAVA反射机制(
Reflection)
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
反射的主要作用:
运行时判断任意一个对象所属的类;在运行时构造任意一个类的对象;在运行时判断任意一个类所具有的成员变量和方法;在运行时调用任意一个对象的方法;生成动态代理
本文以案例驱动的方式,记录一些反射常用的方法,此次记录,练练手,同时作为备忘记录,省的自己每次都得Google.
接口;IPersion
public interface IPersion {
void run();
void walk();
void bus();
void car();
}
测试类:Persion:
public class Persion implements IPersion {
private String name;
private int age;
public Persion() {
}
public Persion(String name, int age) {
this.name = name;
this.age = age;
}
public void sayHello() {
System.out.println(name + " " + age + " say hello!");
return;
}
@Override
public void bus() {
// TODO Auto-generated method stub
}
@Override
public void car() {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
}
@Override
public void walk() {
// TODO Auto-generated method stub
}
//getter and setter
}
【案例1】通过一个对象获得完整的包名和类名
Persion persion = new Persion();
System.out.println(persion.getClass().getName());
结果:
com.xinchun.study.Persion
添加一句:所有类的对象其实都是Class的实例。
【案例2】实例化Class类对象
Class<?> demo1 = null;
Class<?> demo2 = null;
Class<?> demo3 = null;
// 方式一
try {
demo1 = Class.forName("com.xinchun.study.Persion");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 方式二
demo2 = new Persion().getClass();
// 方式三
demo3 = Persion.class;
System.out.println("demo1 类名称" + demo1.getName());
System.out.println("demo2 类名称" + demo2.getName());
System.out.println("demo3 类名称" + demo3.getName());
结果:
demo1 类名称com.xinchun.study.Persion
demo2 类名称com.xinchun.study.Persion
demo3 类名称com.xinchun.study.Persion
【案例3】通过Class实例化其他类的对象
无参构造
// 创建 Class 实例
Class<?> demo = Persion.class;
Persion p = null;
try {
p = (Persion) demo.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
p.setAge(24);
p.setName("newspring");
p.sayHello();
结果:
newspring 24 say hello!
带有参数的构造函数
// 创建 Class 实例
Class<?> demo = Persion.class;
// 取得全部的构造函数
Constructor<?> cons[] = demo.getConstructors();
// 构造函数的个数
System.out.println(cons.length);
try {
Persion p1 = (Persion) cons[0].newInstance();
Persion p2 = (Persion) cons[1].newInstance("newspring", 24);
p1.sayHello();
p2.sayHello();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
结果:
2
null 0 say hello!
newspring 24 say hello!
【案例4】返回一个类实现的接口
// 创建 Class 实例
Class<?> demo = Persion.class;
Persion p = null;
//所有的接口
Class<?> intes[]=demo.getInterfaces();
for (int i = 0; i < intes.length; i++) {
System.out.println("实现的接口 "+intes[i].getName());
}
结果:
实现的接口 com.xinchun.study.IPersion
【案例5】返回一个类继承的父类
//取得父类,有且仅有一个
Class<?> temp=demo.getSuperclass();
结果:
java.lang.Object
【案例6】获全部的构造函数
Constructor<?>[] cons = demo.getConstructors();
for (int i = 0; i < cons.length; i++) {
System.out.println(cons[i]);
}
【案例7】通过class取得一个类的全部框架
本类的成员变量
Field[] field = demo.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
// 权限修饰符
int mo = field[i].getModifiers();
String priv = Modifier.toString(mo);
// 属性类型
Class<?> type = field[i].getType();
System.out.println(priv + " " + type.getName() + " "
+ field[i].getName() + ";");
}
实现接口或者父类的
Field[] filed1 = demo.getFields();
for (int j = 0; j < filed1.length; j++) {
// 权限修饰符
int mo = filed1[j].getModifiers();
String priv = Modifier.toString(mo);
// 属性类型
Class<?> type = filed1[j].getType();
System.out.println(priv + " " + type.getName() + " "
+ filed1[j].getName() + ";");
}
【案例8】通过Class调用类中的方法
// 创建 Class 实例
Class<?> demo = Persion.class;
Method method = demo.getMethod("sayHello");
method.invoke(demo.newInstance());
method = demo.getMethod("sayHello", String.class, int.class);
method.invoke(demo.newInstance(), "newspring", 24);
结果:
null 0 say hello!
newspring 24 say hello!
【案例9】通过Class调用set和get方法
// setter
Object o = demo.newInstance();
Method method = demo.getMethod("set" + "Name", String.class);
method.invoke(o, "newspring");
// getter
method = demo.getMethod("get" + "Name");
System.out.println(method.invoke(o));
结果:
newspring
【案例10】通过反射操作属性
Object o = demo.newInstance();
Field field = demo.getDeclaredField("name");
field.setAccessible(true);
field.set(o, "xinchun");
System.out.println(field.get(o));
结果:
xinchun
【案例11】通过Class实例化其他类的对象
【案例12】通过Class实例化其他类的对象
【案例13】通过Class实例化其他类的对象
【案例14】通过Class实例化其他类的对象