采用接口可以动态实现相同结构的类的实例化/** * @功能描述: 动态实例化事件类 * @param className 类的全路径 * @return flowEvent是className类的接口 */public flowEvent initEvent(String className){ flowEvent event = null; try { event = (flowEvent) Class.forName(className).newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return event;}
也可以使用Object来实例化,此种方式无需依赖自定义接口实现,如:public Object initBusinessDataBean(String className){ Object obj = null; try { obj = Class.forName(className).newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return obj;}