java的装饰设计模式
类似python中的装饰器。
示例:
public class Test5 { public static void main(String[] args) { Worker w = new Worker(new Students()); w.code(); } } interface Coder { public abstract void code(); } class Students implements Coder { @Override public void code() { System.out.println("java se"); System.out.println("java web"); } } class Worker implements Coder{ Students s; public Worker(Students s) { this.s = s; } @Override public void code() { s.code(); System.out.println("Database"); System.out.println("ssm"); System.out.println("ssm"); } } // 输出结果: java se java web Database ssm ssm
代码成就万世基积沙镇海,梦想永在凌云意意气风发。