我是使用eclipse寫的代碼主程序:public class Test { public static void main(String[] args) { try { //動(dòng)態(tài)加載類,在運(yùn)行時(shí)加載 Class<?> c = Class.forName(args[0]); //通過類類型,創(chuàng)建該類對(duì)象 Office test = (Office) c.newInstance(); test.print(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }}package myclass;public interface Office { public void print();}package myclass;public class Word implements Office { @Override public void print() { System.out.println("Word Test!"); }}package myclass;public class Excel implements Office { @Override public void print() { System.out.println("Excel Test!"); }}
1 回答

簡(jiǎn)單丶注定
TA貢獻(xiàn)9條經(jīng)驗(yàn) 獲得超2個(gè)贊
?public static void main(String[] args){
}
要傳入?yún)?shù)給 args[0]
添加回答
舉報(bào)
0/150
提交
取消