一、什么是反射?
- 反射是运行中的程序检查自己和软件运行环境的能力,它可以根据它发现的进行改变。通俗的讲就是反射可以在运行时根据指定的类名获得类的信息。
- JAVA反射机制是在运行状态中,对于任意一个实体类,都能够知道这个类的所有属性和方法;
二、回顾简单工厂模式
/**
* @author lss
* 测试类
*/
public class App1 {
public static void main(String[] args) {
//创建工厂对象
Factory1 factory1 = new Factory1();
Fruit fruit = factory1.getIntence("apple");
fruit.eat();
}
//水果接口
public interface Fruit {
/**
* 水果接口的吃的抽象方法
*/
public void eat();
}
//苹果类实现水果类
public static class Apple implements Fruit{
@Override
public void eat() {
System.out.println("apple eat");
}
}
//橘子类实现水果类
public static class Orange implements Fruit {
@Override
public void eat() {
System.out.println("orange eat");
}
}
//简单工厂
public static class Factory1 {
public Fruit getIntence(String name){
Fruit fruit =null;
if(name.equals("apple")){
fruit = new Apple();
}else if (name.equals("orange")){
fruit = new Orange();
}
return fruit;
}
}
}
通过回顾以上简单工厂,我们可以发现如果我们想要增加一个水果类时,就必须要改写我们的工厂类,那么如何可以在不改变工厂类的情况下新增水果类呢,那么此时我们就需要用到反射(reflect)。
/**
* @author lss
* 测试类
*/
public class App1 {
public static void main(String[] args) {
//创建工厂对象
Factory2 factory2 = new Factory2();
Fruit fruit = factory2.getIntence("com.etc.reflect.Pear");
fruit.eat();
}
}
/**
* @author 梁三岁
* 动态工厂
*/
public class Factory2 {
public Fruit getIntence(String className){
Fruit fruit =null;
try {
fruit= (Fruit)Class.forName(className).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return fruit;
}
}
三、Java的反射机制
- 类加载过程中会创建Class对象用来记录加载类的基本属性和特征。
- 使用Class对象动态的获得某个对象的属性及其方法
- Class对象的常用的方法:
- a)Class.forName(“类的位置”); 返回与带有给定字符串名的类或接口相关联的 Class 对象
- b).newInstance(); 创建实例化对象
- c).getMethods() 取得实例对象中的所有方法(包括继承的方法)
- d) getDeclaredMethods() 取得实例对象中定义的所有方法
4.Method类
a)getName();//获得方法的名称
b)getReturnType();//获得返回类型
c)method.getParameterTypes();//获得参数的类型
数组形式:代表多个参数
d)invoke(Object obj,第二个参数) ---变参方法
- obj:要访问的实例化对象
- 第二个参数 i.没有参数代表从方法中取出数据 ii.带参数,表示向方法中传参
四、Java反射机制练习
/**
* @author 梁三岁
* 汽车类
*/
public class Car {
/**
* 三个私有属性
*/
private String name;
private String color;
private double price;
//方法
public void print(){
System.out.println("汽车信息展示:"+"\n品牌:"+getName()+"\n颜色:"+getColor()+"\n价格:"+getPrice()+"元");
}
public Car() {
}
public Car(String name, String color, double price) {
this.name = name;
this.color = color;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author 梁三岁
* 汽车测试类
*/
public class CarTest {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
Car car = new Car();
car.setName("BYD");
car.setColor("green");
car.setPrice(100000);
Method[] method = car.getClass().getDeclaredMethods();
for (Method m : method) {
String methodName = m.getName();
if (methodName.equals("setColor")) {
m.setAccessible(true);
m.invoke(car, "red");
}
if (methodName.equals("setName")) {
m.setAccessible(true);
m.invoke(car, "Benz");
}
}
Field[] fields = car.getClass().getDeclaredFields();
for (Field f : fields) {
String fieldName = f.getName();
if (fieldName.equals("price")) {
f.setAccessible(true);
f.set(car, 110000);
}
}
car.print();
}
}