1 public class Test_5 { 2 public static void main(String[] args) { 3 you u=new you(); 4 // u.happyMarry(); 5 new weddingCompany(u).happyMarry(); 6 } 7 } 8 //真实对象:你 9 class you implements marry{ 10 @Override 11 public void happyMarry() { 12 System.out.println("你结婚啦~~~~"); 13 } 14 } 15 // 代理对象,婚庆公司 16 class weddingCompany implements marry{ 17 private you u; //代理对象需要一个真实对象 18 public weddingCompany(you u) { 19 this.u = u; 20 } 21 @Override 22 public void happyMarry() { 23 before(); 24 u.happyMarry(); 25 after(); 26 } 27 public void before(){ 28 System.out.println("布置婚礼"); 29 } 30 public void after(){ 31 System.out.println("给钱~~~"); 32 } 33 } 34 // 共同接口:结婚 35 interface marry{ 36 void happyMarry(); 37 }
原文地址:https://www.cnblogs.com/xbfchder/p/10961519.html
时间: 2024-10-29 09:18:38