我是使用eclipse写的代码
主程序:
public class Test {
public static void main(String[] args) {
try {
//动态加载类,在运行时加载
Class<?> c = Class.forName(args[0]);
//通过类类型,创建该类对象
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!");
}
}
简单丶注定
相关分类