首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

简略的工厂模试

2012-10-07 
简单的工厂模试//动物类接口interface Animal{//发出声音public class sayHello(){}}//生产动物的工厂类c

简单的工厂模试

//动物类接口      interface Animal{          //发出声音          public class sayHello(){};      }           //生产动物的工厂类      class AnimalFactory{          public static Animal createDog(){              return (Animal)new Dog();          }      }           //狗实现动物接口      class Dog implements Animal{          //狗会汪汪叫          public class sayHello(){              System.out.println("汪汪");          }      }      //外国狗实现动物接口      class EnglishDog implements Animal{          //外国狗会Wolf!Wolf!叫          public class sayHello(){              System.out.println("Wolf!Wolf!");          }      }        //测试用   class Test(){       public static void main(String [] args){              //从工厂类里面获取一只狗              Animal animal1 = AnimalFactory.createDog();             animal1.sayHello();          }   }  

热点排行