利用简单工厂设计模式参考以下给定的代码设计一个测试程序。 interface Fruit 定义一个水果的接口 public void eat ; 吃水果的方法 class Apple implements Fruit 定义子类 —— Apple public void eat ……………… class Orange implements Fruit 定义子类 —— Oran
public class Test { public static void main(String[] args) { Fruit apple = Factory.getInstance("apple");// 通过工厂类获取Apple实例 apple.eat();// 调用eat方法 Fruit orange = Factory.getInstance("orange");// 通过工厂类获取Orange实例 orange.eat();// 调用eat方法 } }
原文地址: https://www.cveoy.top/t/topic/bsLJ 著作权归作者所有。请勿转载和采集!