021.7 装饰设计模式
Posted Alos
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了021.7 装饰设计模式相关的知识,希望对你有一定的参考价值。
解决问题 :给对象提供额外的功能(职责),比继承更灵活
public class PersonDemo { public static void main(String[] args) { Person p = new Person(); NewPerson np = new NewPerson(p); np.eat(); } } class Person{ void eat(){ System.out.println("eat"); } } //装饰器 class NewPerson{ private Person p; public NewPerson(Person p){ this.p = p; } void eat(){ System.out.println("开胃"); p.eat(); System.out.println("甜点"); } } //继承 class SubPerson extends Person{ void eat(){ System.out.println("开胃"); super.eat(); System.out.println("甜点"); } }
以上是关于021.7 装饰设计模式的主要内容,如果未能解决你的问题,请参考以下文章