JAVA中设计模式

2025-05-25 18:19:18

1、观察下面程序:定义一个接口interface Fruit{ public void eat();}主方法实际上就相当于一个客户端;

JAVA中设计模式

2、class Apple implements Fruit{ public void eat(){ System.out.println("吃苹果");}}

JAVA中设计模式

3、clas衡痕贤伎s Orange implements Fruit { public void eat(){ System.out.printl荏鱿胫协n("吃橘子");}}public class interfaceDemo(){ public static void main(String args[]){ Fruit f=new Apple(); f.eat();}}实现Fruit接口,如果此时需要更换一个子类,子必须要修改主方法

JAVA中设计模式
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢