1 //租房接口 2 public interface Rent { 3 public void rent(); 4 } 5 //主人去实现租房接口 6 public class Host implements Rent { 7 @Override 8 public void rent() { 9 System.out.println("房东要出租房子"); 10 } 11 } 12 //代理类 13 public class Proxy implements Rent{ 14 private Host host; 15 //有参 16 public Proxy(Host host){ 17 this.host=host; 18 } 19 20 @Override 21 public void rent() { 22 //代理直接掉host的方法 进行代理 23 host.rent(); 24 seeHouse(); 25 Money(); 26 } 27 28 public void seeHouse(){ 29 System.out.println("看房子"); 30 } 31 public void Money(){ 32 System.out.println("谈金钱"); 33 } 34 } 35 public class Proxy implements Rent{ 36 private Host host; 37 //有参 38 public Proxy(Host host){ 39 this.host=host; 40 } 41 @Override 42 public void rent() {睿共享 43 //代理直接掉host的方法 进行代理 44 host.rent(); 45 seeHouse(); 46 Money(); 47 } 48 //代理可以扩展其他功能 49 public void seeHouse(){ 50 System.out.println("看房子"); 51 } 52 public void Money(){ 53 System.out.println("谈金钱"); 54睿共享 }睿共享 55 } 56 57 public class Client { 58 public static void main(String[]args){ 59 //需要把房东new出来 60 Host host = new Host(); 61 //把房东拿给代理 让代理直接掉房东的方法 62 Proxy proxy睿共享 = new Proxy(host); 63 proxy.rent();睿共享 64 } 65 }