您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

简单工厂Factory

2024/4/13 15:34:39发布23次查看
package net.util; /** * @项目名:spring2.5 * @包名:net.util * @文件名:factorydemo.java * @日期:jun 21, 2011 4:37:02 pm * @备注:工厂模式 * @作者:apple */ public class factorydemo { /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub car c=factory.getcarinstance("banz"); if(c!=null){ c.run(); c.stop(); } else{ system.out.println("制造不了"); } } } class factory{ public static car getcarinstance(string type){ car c=null; try { c=(car)class.forname("net.util."+type).newinstance(); } catch (instantiationexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalaccessexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } return c; } } interface car{ public void run(); public void stop(); } class banz implements car{ public void run() { // todo auto-generated method stub system.out.println("banz跑"); } public void stop() { // todo auto-generated method stub system.out.println("banz停"); } } class ford implements car{ public void run() { // todo auto-generated method stub system.out.println("ford跑"); } public void stop() { // todo auto-generated method stub system.out.println("ford停"); } }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product