1. 反射机制
标准:
package _1._Agent.Reflect._DynamicLoading; public interface OfficeAble { void start(); }
标准实现:
package _1._Agent.Reflect._DynamicLoading; public class Word implements OfficeAble { @Override public void start() { System.out.println("Word...start..."); } }
类:
package _1._Agent.Reflect._DynamicLoading; //通过Office类启动word或者excel public class Office { public static void main(String[] args) { try { //如果要用的类没有,只会在运行时候出错,不会再编译时候出问题 Class c = Class.forName("_1._Agent.Reflect._DynamicLoading.Word"); //动态加载一个类,让这些类实现统一的标准,OfficeAble OfficeAble oa = (OfficeAble)c.newInstance(); oa.start(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } } }
2. JDK动态代理