zoukankan      html  css  js  c++  java
  • Java之工厂模式

    interface Fruit {
        void eat();
    }

    class Apple implements Fruit {

        public void eat() {
            System.out.println("I am eating apple.");
        }

    }

    class Orange implements Fruit {

        public void eat() {
            System.out.println("I am  eating orange.");
        }

    }

    class Factory {
        public Fruit getInstance(String className) {
            Fruit f = null;
            if ("apple".equals(className)) {
                f = new Apple();
            } else {
                f = new Orange();
            }
            return f;
        }
    }

    public class InterfaceDemo04 {
        public static void main(String[] args) {
            Factory factory = new Factory();
            Fruit f = null;
            f = factory.getInstance("apple");
            f.eat();
        }
    }

  • 相关阅读:
    yii中通过HTTP post接收
    网络编程
    python 异常处理
    面向对象(2)
    面向对象
    什么是模块精讲
    常用模块二
    各种推导式详解
    匿名函数
    迭代器生成器
  • 原文地址:https://www.cnblogs.com/vonk/p/3889562.html
Copyright © 2011-2022 走看看