Java反射机制
2018-09-20 本文已影响4人
韭菜待收割
1、反射机制的含义
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
2、反代码示例
//懒,就写一起了
public class MainTest {
//测试方法
public static void main(String[] args) throws Exception {
MainTest mainTest = new MainTest();
//得到某个对象的公共属性
User user = new User();
user.setAge(100);
Object propertyAge = mainTest.getProperty(user, "age");
System.out.println("得到某个对象的公共属性 propertyAge=" + propertyAge);
//得到某类的静态公共属性
Object propertyName = mainTest.getStaticProperty("User", "name");
System.out.println("得到某类的静态公共属性 propertyName=" + propertyName);
//执行某对象方法
mainTest.invokeMethod(user, "printUser", new String[]{"xmy"});
//执行某类的静态方法
mainTest.invokeStaticMethod("User", "printStaticUser", new String[]{"staticxmy"});
//新建实例
mainTest.newInstance("User", new Object[0]);//默认构造函数
Object[] aa = {1, 2};
mainTest.newInstance("User", aa);
//判断是不是某个类的实例
System.out.println("test是不是user的实例:" + mainTest.isInstance("test", User.class));
System.out.println("user是不是user的实例:" + mainTest.isInstance(user, User.class));
//打破封装
try {
Class c = Class.forName("User");
//获取privateid属性
Field field = c.getDeclaredField("id");
//实例化这个类赋给o
Object object = c.newInstance();
//打破封装
field.setAccessible(true); //使用反射机制可以打破封装性,导致了java对象的属性不安全。
//给object对象的id属性赋值"110"
field.set(object, 110);
System.out.println("private int id=" + field.get(object));
} catch (Exception e) {
e.printStackTrace();
}
}
//是不是某个类的实例
public boolean isInstance(Object obj, Class cls) {
return cls.isInstance(obj);
}
//新建实例
public Object newInstance(String className, Object[] args) throws Exception {
Class newoneClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Constructor cons = newoneClass.getConstructor(argsClass);
return cons.newInstance(args);
}
//执行某类的静态方法
public Object invokeStaticMethod(String className, String methodName,
Object[] args) throws Exception {
Class ownerClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(null, args);
}
//执行某对象方法
public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
Class ownerClass = owner.getClass();
Class[] argsClasses = new Class[args.length];
for (int i = 0; i < args.length; i++) {
argsClasses[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClasses);
return method.invoke(owner, args);
}
//得到某类的静态公共属性
public Object getStaticProperty(String className, String fieldName) throws Exception {
Class ownerClass = Class.forName(className);
Field field = ownerClass.getField(fieldName);
Object property = field.get(ownerClass);
return property;
}
//得到某个对象的公共属性
public Object getProperty(Object owner, String fieldName) throws Exception {
Class ownerClass = owner.getClass();
Field field = ownerClass.getField(fieldName);
Object property = field.get(owner);
return property;
}
}
//
public class User {
private int id;
public int age;
public static String name = "xmy";
public User(Integer id, Integer age) {
this.id = id;
this.age = age;
System.out.println("对象参数 我是带参构造方法:id= " + id + "; age=" + age);
}
//这个不可以 参数不是object
public User(int id, int age) {
this.id = id;
this.age = age;
System.out.println("基本类型参数 我是带参构造方法:id=" + id + "; age=" + age);
}
public User() {System.out.println("我是默认构造方法 ");}
public void printUser(String test) {System.out.println("my name is " + test);}
public static void printStaticUser(String test) {System.out.println("my static name is " + test);}
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}
public static String getName() {return name;}
public static void setName(String name) {User.name = name;}
}