把Class.forName()换一种写法就报错了!
1 首先,这段代码没有任何问题。 public class OfficeBetter{ public static void main(String[] args){ try{ OfficeAble oa =(OfficeAble) Class.forName(args[0]).getConstructor().newInstance(); oa.start(); }catch(Exception e){ e.printStackTrace(); } } }
2 可是,当我不想用
Class.forName(args[0])
来获取类类型,改成这种写法就报错了:
public class OfficeBetter{ public static void main(String[] args){ try{ Class c=Class.forName(args[0]); Constructor constructor=c.getConstructor(); OfficeAble oa=(OfficeAble)constructor.newInstance(); oa.start(); }catch(Exception e){ e.printStackTrace(); } } }
谁能帮我分析一下吗?编译报错:
OfficeBetter.java:5: error: cannot find symbol Constructor constructor=c.getConstructor(); ^ symbol: class Constructor location: class OfficeBetter Note: OfficeBetter.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error