Java反射为开发者提供了更灵活的方式编程,可以在运行时操纵类的构造方法、属性和方法。
1、调用类的公有方法
Car类作为通过反射获取其isExpensive()方法
package com.reflect; public class Car { private String name; private Long price; public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getPrice() { return price; } public void setPrice(Long price) { this.price = price; } public boolean isExpensive(Long price) { if(price>200000L){ System.out.println("this car price is more than 20w!"); return true; } else { System.out.println("this car price is less than 20w!"); return false; } } }写一个方法进行反射调用
public class ReflectTest { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException { Class<?> myTest = Class.forName("com.discover.amazing.reflect.Car"); Method fun1 = myTest.getMethod("isExpensive", Long.class); boolean result = (Boolean) fun1.invoke(myTest.newInstance(), 500000L); } }2、操纵类的属性
修改Car类的isExpensive()方法
public class Car { private String name; private Long price; public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getPrice() { return price; } public void setPrice(Long price) { this.price = price; } public boolean isExpensive() { if(this.getPrice()>200000L){ System.out.println("this car price is more than 20w!"); return true; } else { System.out.println("this car price is less than 20w!"); return false; } } }通过反射获取类的属性,然后设置该属性的值
public class ReflectTest { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchFieldException { Class<?> car = Class.forName("com.discover.amazing.reflect.Car"); Method fun1 = car.getMethod("isExpensive"); Field priceField = car.getDeclaredField("price"); Object carInstance = car.newInstance(); priceField.setAccessible(true); priceField.set(carInstance, 150000L); boolean result = (Boolean) fun1.invoke(carInstance); } }3、操纵类的构造方法
在Car类中增加构造方法
public class Car { private String name; private Long price; public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getPrice() { return price; } public void setPrice(Long price) { this.price = price; } public Car(String name, Long price) { this.name = name; this.price = price; } public boolean isExpensive() { if(this.getPrice()>200000L){ System.out.println("this car price is more than 20w!"); return true; } else { System.out.println("this car price is less than 20w!"); return false; } } }通过反射获取构造方法,通过构造方法实例化一个Car对象,然后调用Car的 isExpensive()方法
public class ReflectTest { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchFieldException { Class<?> clazz = Class.forName("com.discover.amazing.reflect.Car"); Constructor<?> carConstructor = clazz.getDeclaredConstructor(String.class, Long.class); carConstructor.setAccessible(true); Car car = (Car) carConstructor.newInstance("Benz", 500000L); car.isExpensive(); } }