谈资不是这个p.eat(); 这个p 是 private Person p; 吗?
- /**
- 装饰设计模式:增加原有类的功能
- **/
- class RedoublePerson
- {
- public static void main(String[] args)
- {
- Person p=new Person();
- RePerson rp=new RePerson(p);
- rp.reEat();
- }
- }
- class Person
- {
- public void eat ()
- {
-
- System.out.println("吃饭");
- }
- }
- class RePerson//Person 类加强
- {
- private Person p;
- public RePerson(Person p)
- {
- this.p=p;
-
- }
- public void reEat()
- {
- System.out.println("开胃酒");
- p.eat();
- System.out.println("喝点营养汤");
- System.out.println("抽一根烟");
-
- }
- }
复制代码
|
|