装饰设计模式优点:耦合性不强,被装饰的类的变化与装饰类的变化无关
public static void main (String[] args){
HeiMaStudent hms = new HeiMaStudent (new Student());
hms.code();
}
interface Code{
public void code();
}
class Student implements Coder{
@Override
public void code(){
syso("javase");
syso("javeweb");
}
}
class HeiMaStudent implements Coder{
//1.获取被装饰类的引用
parivate Student s; //获取学生引用
//2.在构造方法中传入被装饰类的对象
public HeiMaStudent(Student s){
this.s = s;
}
//对原有的功能进行升级
@Override
public void code(){
s.code();
syso("数据库");
syso("ssh");
syso("大数据");
syso("AI智能");
}
}