黑马程序员-JAVA高级视频_IO输入与输出-19天-5(装饰设计模式)

package itcast.java16;

/*
 * 可以定义一个类,将已有对象传入,基于已有的功能,并提供加强功能
 * 那么自定义的该类称为装饰类
 * 
 * 装饰类通常会通过构造方法接受被装饰的对象
 * 并基于被装饰的对象的功能,提供更强的功能
 */

class Person {
    public void eat() {
        System.out.println("吃饭");
    }
}

class SuperPerson {
    private Person p;

    public SuperPerson(Person p) {
        this.p = p;
    }

    public void superEat() {
        System.out.println("起床");
        p.eat();
        System.out.println("喝汤");
    }
}

public class DecorateDemo {
    public static void main(String[] args) {
        SuperPerson superPerson = new SuperPerson(new Person());
        superPerson.superEat();
    }

}

 

posted @ 2013-03-21 23:29  谷文仁  阅读(151)  评论(0编辑  收藏  举报