java反射

2020-10-19  本文已影响0人  陈萍儿Candy

可以通过类的全名,获取类,通过c.newInstance()创建此类的对象;
Class c = null;
try {
c = Class.forName("interfacetest.Worker");
Log.i("cyp","c:"+c);
Object o = c.newInstance();
IWork work = (IWork) o;
work.startWork("周五周五周五开心下班");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}

上一篇下一篇

猜你喜欢

热点阅读