Java反射原理

Java的反射机制允许我们动态的调用某个对象的方法/构造方法、获取某个对象的属性,而无需在编码时确定调用的对象,这种机制在我们常用的框架中常见。

1,原理

Class actionClass = Class.forName("MyClass");    // 类的装载与链接

Object action = actionClass.newInstance();          //初始化对象

Method method = actionClass.getMethod("myMethod",null);    //获取类的方法

method.invoke( action ,null);    // 方法引用

2,获取Method对象

3,调用invoke()方法

猜你喜欢

转载自my.oschina.net/u/2320584/blog/1784819