装饰者模式是为了增加功能,传入一个原来类的对象,一般来说继承同样的接口,实现相同的功能
package com.zhuanshi;
class Person{
public void chifan() {
System.out.println("吃饭");
}
}
class SuperPerson{
Person p;
public SuperPerson(Person p){
this.p=p;
}
public void superchiafan() {
p.chifan();
System.out.println("来一杯");
System.out.println("饭后甜点");
}
}
public class ZhuanShi {
public static void main(String[] args) {
Person p=new Person();
SuperPerson pp=new SuperPerson(p);
pp.superchiafan();
}
}