------<a href="
http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
以前没听说过装饰设计模式,现在学完感觉特别灵活。
因为装饰类是为了增强已有对象,具备的功能和已有对象的功能是一样的,但是更强,所以装饰类和被装饰类通常都属于一个体系中的。
比如:
class Person(){
public void chifan(){
System.out.println("吃饭");
}
}
当以后不仅仅满足于吃饭的时候,不能更改源码,那么就有了装饰类来增强
class MyPerson() extends Person{
private Person p = null;
public MyPerson(Person p){
this.p=p;
}
public void MyChifan(){
System.out.println("喝酒");
p.chifan();
System.out.println("吃饭抽烟");
}
}
这样做也降低了类与类之间的关系,如果现在的装饰类有问题,那么也可以直接用原有类的方法。
------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------