读书人

透过获取对象的构造方法建立对象

发布时间: 2014-01-01 18:12:08 作者: rapoo

通过获取对象的构造方法建立对象
getConstructor
getConstructors
getDeclaredConstructors
con.newInstance();
con.setAccessible(true);//暴力访问,临时修改修饰符 私有构造函数/方法

clazz.getMethods();
clazz.getDeclaredMethods();
clazz.getMethod("show",int.class)//先获取方法
获取对象构造getConstrutcors,建立对象newInstances通过对象调用方法
...method.invoke


properties文件键值读取
properties.load(fr);//FileReader fr;
properties.getProperties(key)

软编码,通过反射的方式解决配对问题
Class clazz = Class.forName("abc.ab");
Constructor con = clazz.getConstructor();
Method method = clazz.getMethod();
Object obj = con.newInstance();
method.setAccessible(true);
method.invoke();

Properties pro = Properties();
FileReader fr = new FileReader("C:\\1.properties");
pro.load(fr);
for(int x = 0;x<pro.size()/2;x++)
{
pro.getProperties(key);//从配置文件中获取类名和方法名来执行相应的方法
}
反射
什么是泛型擦除?

读书人网 >编程

热点推荐