编程语言
首页 > 编程语言> > Java反射——实现动态代理

Java反射——实现动态代理

作者:互联网

1.Java静态代理举例:
代理类和被代理类在编译期间就已经确定下来了
 1 interface ClothFactory{
 2     void produceCloth();
 3 }
 4 
 5 class ProxyClothFactory implements ClothFactory{
 6     private ClothFactory factory;
 7     public ProxyClothFactory(ClothFactory factory) {
 8         this.factory = factory;
 9     }
10 
11     public void produceCloth() {
12         System.out.println("准备一些工作");
13         factory.produceCloth();
14         System.out.println("准备一些后续工作");
15     }
16 }
17 
18 class NikeClothFactory implements ClothFactory{
19     @Override
20     public void produceCloth() {
21         System.out.println("Nike工厂生产一批衣服");
22     }
23 }
24 
25 public class StaticProxyTest    {
26     public static void main(String[] args) {
27         //创建被代理类对象
28         NikeClothFactory nike = new NikeClothFactory();
29         //创建代理类对象
30         ProxyClothFactory proxyClothFactory = new ProxyClothFactory(nike);
31         proxyClothFactory.produceCloth();
32     }
33 }

 

标签:反射,Java,void,代理,factory,ClothFactory,produceCloth,public,ProxyClothFactory
来源: https://www.cnblogs.com/jue1e0/p/16466559.html