首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2014-01-01 
通过获取对象的构造方法建立对象getConstructorgetConstructorsgetDeclaredConstructorscon.newInstance()

通过获取对象的构造方法建立对象
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);//从配置文件中获取类名和方法名来执行相应的方法
}
反射
什么是泛型擦除?

热点排行