标签:ide contract void 测试 rri pack imp color div
静态代理有三要素:1,共同实现的接口 2,真实成员 3,代理成员
共同实现的接口:Star.java
1 package cn.ftf.proxy; 2 3 public interface Star { 4 void confer(); 5 void signContract(); 6 void bookTicket(); 7 void sing(); 8 void collect(); 9 }
真实成员:RealStar.java
1 package cn.ftf.proxy; 2 3 public class RealSrar implements Star{ 4 5 @Override 6 public void confer() { 7 // TODO Auto-generated method stub 8 System.out.println(" confer()"); 9 10 } 11 12 @Override 13 public void signContract() { 14 // TODO Auto-generated method stub 15 System.out.println("signContract()"); 16 17 } 18 19 @Override 20 public void bookTicket() { 21 // TODO Auto-generated method stub 22 System.out.println("bookTicket()"); 23 } 24 25 @Override 26 public void sing() { 27 // TODO Auto-generated method stub 28 System.out.println("sing()周杰伦"); 29 } 30 31 @Override 32 public void collect() { 33 // TODO Auto-generated method stub 34 System.out.println("collect()"); 35 36 } 37 }
代理成员ProxyStar.java
1 package cn.ftf.proxy; 2 3 public class ProxyStar implements Star{ 4 5 private Star star; 6 7 public ProxyStar(Star star) { 8 super(); 9 this.star = star; 10 } 11 12 @Override 13 public void confer() { 14 // TODO Auto-generated method stub 15 System.out.println("maintan"); 16 17 } 18 19 @Override 20 public void signContract() { 21 // TODO Auto-generated method stub 22 System.out.println("qianxieyi"); 23 } 24 25 @Override 26 public void bookTicket() { 27 // TODO Auto-generated method stub 28 System.out.println("maipiao"); 29 } 30 31 @Override 32 public void sing() { 33 // TODO Auto-generated method stub 34 star.sing(); 35 } 36 37 @Override 38 public void collect() { 39 // TODO Auto-generated method stub 40 System.out.println("shouweikuan"); 41 } 42 43 }
测试Test.java
1 package cn.ftf.proxy; 2 3 public class Test { 4 public static void main(String[] args) { 5 Star real=new RealSrar(); 6 Star proxy = new ProxyStar(real); 7 8 proxy.confer(); 9 proxy.bookTicket(); 10 proxy.sing(); 11 proxy.collect(); 12 13 } 14 }
运行结果:
maintan
maipiao
sing()周杰伦
shouweikuan
标签:ide contract void 测试 rri pack imp color div
原文地址:https://www.cnblogs.com/fangtingfei/p/11247417.html