是为了增强某个对象的功能,装饰和继承都能实现增强功能,装饰可以防止继承体系过于庞大
package cn.itcast.wrapper;
/**
*
* @author Administrator
*
*/
public class Wrapper {
public static void main(String[] args) {
Person p = new Person();
//p.eat();
NewPerson p1 = new NewPerson(p);
p1.eat();
}
}
class Person{
public void eat(){
System.out.println("eat things");
}
}
//为了增强Person
class NewPerson{
private Person p;
NewPerson(Person p){
this.p = p;
}
public void eat(){
System.out.println("eat fruits");
p.eat();
}
}