}
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();