zoukankan      html  css  js  c++  java
  • 简单工厂代码演示

     1 package com.wisezone.poly;
     2 
     3 /**
     4  * 猫
     5  * @author 王东海
     6  * @2017年4月16日
     7  */
     8 public class Cat extends Animal
     9 {
    10 
    11     @Override
    12     public void eat()
    13     {
    14         System.out.println("猫吃鱼。。。");
    15     }
    16 
    17     @Override
    18     public void run()
    19     {
    20         System.out.println("猫跑。。。");
    21     }
    22     
    23 }
     1 package com.wisezone.poly;
     2 
     3 /**
     4  * 狗
     5  * @author 王东海
     6  * @2017年4月16日
     7  */
     8 public class Dog extends Animal
     9 {    
    10     @Override
    11     public void eat(){
    12         System.out.println("狗吃骨头。。。");
    13     }
    14     
    15     @Override
    16     public void run(){
    17         System.out.println("狗跑。。。");
    18     }
    19     
    20     //新增方法
    21     public void watch(){
    22         System.out.println("狗看家。。。");
    23     }
    24 }
     1 package com.wisezone.poly;
     2 
     3 /**
     4  * 动物
     5  * @author 王东海
     6  * @2017年4月16日
     7  */
     8 public class Animal
     9 {
    10     public void eat(){
    11         System.out.println("动物吃。。。");
    12     }
    13     
    14     public void run(){
    15         System.out.println("动物跑。。。。");
    16     }
    17 }
     1 package com.wisezone.poly;
     2 /**
     3  * 简单工厂
     4  * @author 王东海
     5  * @2017年4月16日
     6  */
     7 public class Factory
     8 {
     9     public static Animal product(String type){
    10         if (type.equals("dog"))
    11         {
    12             return new Dog();
    13         }else if (type.equals("cat")) {
    14             return new Cat();
    15         }
    16         return new Animal();
    17     }
    18     
    19     
    20     public static void main(String[] args)
    21     {
    22         Animal an = Factory.product("cat");
    23         an.eat();
    24     }
    25 }

    结果为:

    猫吃鱼。。。

  • 相关阅读:
    sqlhelper类
    嵌入式的n个方向
    study vim + cscope
    mail lists
    关于我的学习
    yahoo enter linux mobile competition
    找工作啦 啦啦啦啦啦
    minicom display unsolicited codes
    并购的年代
    配置rt73无线网卡至suse10.3
  • 原文地址:https://www.cnblogs.com/wdh1995/p/6718562.html
Copyright © 2011-2022 走看看