最近在研究反射,在百度上看了几遍关于反射的文章,最后查看Jdk 关于反射的API,结合自己的理解分享给大家;
反射原理(引入JDK中的官方说明):反射允许编程访问关于加载类的字段、方法和构造方法的信息,并允许使用反射字段、方法和构造方法对其底层对等项进行操作。
主要是通过 class 返回类的 字段、方法、构造方法,如 Spring 的工厂模式就是通过反射来实例化类的;
1、返回类的所有方法:
Class<?> name = Class.forName("com.chinadatapay.entity.Activity");// 通过包名的类名获取类中方法
Method[] methods = name.getMethods();
for (Method field : methods) {
System.out.println("-->"+field);
}
红框中的方法是继承 java object 中的方法
2、获取类的构造方法:
Class<?> name = Class.forName("com.chinadatapay.entity.Activity");
Constructor<?>[] constructors = name.getConstructors();
for (Constructor constructor : constructors) {
System.out.println("-->"+constructor);
}
如果类中没有写构造方法,会获取类的无参构造
3、获取类中所有属性:
Class<?> name = Class.forName("com.chinadatapay.entity.Activity");
Field[] declaredFields = name.getDeclaredFields();
for (Field field : declaredFields) {
System.out.println("-->"+field);
}
4、通过反射实例化类:
Class<?> name = Class.forName("com.chinadatapay.entity.Activity");
Activity activity = (Activity) name.newInstance();
最后贴程序:
package reflect;
/**
*
* @Description 水果接口
* @author <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
* @date 2016-12-21上午11:01:16
*/
public interface IFruit {
/**
*
* @Description 实例化水果接口
* @author <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
* @date <p style="color:#000;font-family:微软雅黑;font-size=16px;">2016-12-21上午11:02:22</p>
*/
public void newInstanceFruit();
}
package reflect;
/**
*
* @Description 苹果
* @author <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
* @date 2016-12-21上午10:53:01
*/
public class Apple implements IFruit{
/**
*
* @Description 实例化苹果
* @author <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
* @date <p style="color:#000;font-family:微软雅黑;font-size=16px;">2016-12-21上午11:34:16</p>
* @return 苹果
*/
@Override
public void newInstanceFruit() {
System.out.println("苹果实例化成功...");
}
}
package reflect;
/**
*
* @Description 橘子
* @author <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
* @date 2016-12-21上午10:52:44
*/
public class Orange implements IFruit{
/**
*
* @Description 实例化橘子
* @author <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
* @date <p style="color:#000;font-family:微软雅黑;font-size=16px;">2016-12-21上午11:32:11</p>
* @return 橘子对象
*/
@Override
public void newInstanceFruit() {
System.out.println("橘子实例化成功...");
}
}
package reflect;
/**
*
* @Description 水果工厂
* @author <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
* @date 2016-12-21上午10:53:59
*/
public final class FruitFactory {
/**
*
* @Description 实例化水果
* @author <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
* @date <p style="color:#000;font-family:微软雅黑;font-size=16px;">2016-12-21上午11:14:11</p>
* @param fruitType 水果种类
* @return
*/
public static IFruit produceFruit(String fruitType) {
IFruit fruit = null;
try {
fruit = (IFruit) Class.forName(fruitType).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return fruit;
}
}
package reflect;
/**
*
* @Description 反射测试类
* @author <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
* @date 2016-12-21上午11:50:57
*/
public class Test {
public static void main(String[] args) {
IFruit apple = FruitFactory.produceFruit("reflect.Apple");
apple.newInstanceFruit();
System.out.println(apple);
IFruit orange = FruitFactory.produceFruit("reflect.Orange");
orange.newInstanceFruit();
System.out.println(orange);
}
}
最终运行结果:
苹果实例化成功...
reflect.Apple@25e4e6db
橘子实例化成功...
reflect.Orange@6cd8f317