关于反射的基本介绍在我之前博客讲到过所以在这里不再多讲
class Stuent {
private String name = "Listen";
public int age = 10;
private Stuent(String name, int age) {
this.name = name;
this.age = age;
}
public Stuent() {
}
public Stuent(String name) {
this.name = name;
}
private void play(String game) {
System.out.println(this.name + " 在玩" + game);
}
public void eat(String food, String food2) {
System.out.println(this.name + " 在吃" + food + food2);
}
@Override
public String toString() {
return "Stuent{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
接下来我们开始使用反射,我们依旧反射上面的Student类,把反射的逻辑写到另外的类当中进行理解
注意:所有和反射相关的包都在 import java.lang.reflect包下面。
利用反射调用公有构造方法创建对象
public static void reflectNewInstance() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Class<?> classStudent = Class.forName("Stuent");
//注意newInstance里传入的参数
Object sudentInstance = classStudent.newInstance();
Stuent stuent = (Stuent) sudentInstance;
System.out.println(stuent);
//第二种
Stuent stuent1 = (Stuent) classStudent.newInstance();
System.out.println(stuent1);
//第三种
Constructor<?> constructor = classStudent.getDeclaredConstructor(String.class);
Stuent stuent2 = (Stuent) constructor.newInstance("Bike");
System.out.println(stuent2);
}
通过反射调动私有构造方法创建对象
public static void reflectPrivateConstructor() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Class<?> sutndetClass = Class.forName("Stuent");
Constructor<?> studentConstructor= sutndetClass.getDeclaredConstructor(String.class, int.class);
//获得权限,破坏private封装
studentConstructor.setAccessible(true);
Stuent stuent = (Stuent) studentConstructor.newInstance("Bike", 20);
System.out.println(stuent);
}
反射私有属性
public static void reflectPrivateField() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
Class<?> studentCalss = Class.forName("Stuent");
Field field = studentCalss.getDeclaredField("name");
//获得破坏private封装的权限
field.setAccessible(true);
Stuent stuent = new Stuent();
//可以通过set来修改,也可以通过get来获取
System.out.println(stuent);
field.set(stuent,"Frake");
String name = (String) field.get(stuent);
System.out.println(name);
}
反射私有方法
public static void reflectPrivateMethod() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<?> studentClass = Class.forName("Stuent");
Method method = studentClass.getDeclaredMethod("play", String.class);
//破坏private权限
method.setAccessible(true);
Stuent stuent = new Stuent();
method.invoke(stuent, " Baketball");
//如果不是私有方法
Method method1 = studentClass.getDeclaredMethod("eat", String.class, String.class);
method1.invoke(stuent, " Apple", " Banana");
}
反射优点和缺点
优点
- 对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法
- 增加程序的灵活性和扩展性,降低耦合性,提高自适应能力
- 反射已经运用在了很多流行框架如:Struts、Hibernate、Spring 等等。
缺点
- 使用反射会有效率问题。会导致程序效率降低。
- 反射技术绕过了源代码的技术,因而会带来维护问题。反射代码比相应的直接代码更复杂 。
测试
public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
reflectNewInstance();
System.out.println("=============");
reflectPrivateConstructor();
System.out.println("=============");
reflectPrivateField();
System.out.println("=============");
reflectPrivateMethod();
}
结果:
Stuent{name='Listen', age=10}
Stuent{name='Listen', age=10}
Stuent{name='Bike', age=10}
=============
Stuent{name='Bike', age=20}
=============
Stuent{name='Listen', age=10}
Frake
=============
Listen 在玩 Baketball
Listen 在吃 Apple Banana