标签:voc get org 方法 import instance row throws owa
package com.liubingfei.test.proxy.staticproxy; /** * @author LiuBingFei * @desc 接口,定义抽象方法:卖房子,出租房子。 * @date 2020/3/5 22:09 * @return */ public interface house { //卖房子 public abstract void sell_house(); //出租房子 public abstract void rental_house(); }
package com.liubingfei.test.proxy.staticproxy; /** * @author LiuBingFei * @desc * @date 2020/3/5 22:12 * @return */ public class house_owner implements house{ @Override public void sell_house() { System.out.println("房主卖房子"); } @Override public void rental_house() { System.out.println("房主出租房子"); } }
package com.liubingfei.test.proxy.staticproxy; /** * @author LiuBingFei * @desc * @date 2020/3/5 22:14 * @return */ public class house_proxy implements house { //房主 private house_owner owner; public house_proxy(house_owner owner) { this.owner = owner; } @Override //调用房主卖房子 public void sell_house() { owner.sell_house(); } @Override //调用房主出租房子 public void rental_house() { owner.rental_house(); } }
package com.liubingfei.test.proxy.staticproxy; import org.junit.Test; /** * @author LiuBingFei * @desc * @date 2020/3/5 22:19 * @return */ public class static_proxy { @Test //静态代理:房产中介house_proxy代理房主house_owner卖房子 //静态代理:代理类和被代理类,实现同样的接口或者继承同样的父类。在程序运行前就编译好,叫做静态代理。 public void proxy() { house_proxy proxy = new house_proxy(new house_owner()); proxy.sell_house(); proxy.rental_house(); } }
package com.liubingfei.test.proxy.dynamicproxy.jdk_proxy; /** * @author LiuBingFei * @desc 接口,定义抽象方法:卖房子,出租房子。 * @date 2020/3/5 22:09 * @return */ public interface House { //卖房子 public abstract void sell_house(); //出租房子 public abstract void rental_house(); }
package com.liubingfei.test.proxy.dynamicproxy.jdk_proxy; /** * @author LiuBingFei * @desc * @date 2020/3/5 22:12 * @return */ public class House_owner implements House{ @Override public void sell_house() { System.out.println("房主卖房子"); } @Override public void rental_house() { System.out.println("房主出租房子"); } }
package com.liubingfei.test.proxy.dynamicproxy.jdk_proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * @author LiuBingFei * @desc * @date 2020/3/5 22:48 * @return */ public class House_proxy implements InvocationHandler { private House house; public House_proxy(House house) { this.house = house; } /** * Proxy.newProxyInstance(param1, param2, param3) * param1: ClassLoader对象; * param2: 被代理类的接口数组,因为代理底层要根据接口生成; * param3:参数类型是InvocationHandler即可,this即是当前类,它实现了这个接口。 * @return 返回一个代理类 */ public Object getProxy(){ Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{House.class}, this); return proxy; } /** * * @param proxy 代理对象,就是房产中介 * @param method 代理对象中的方法 * @param args 代理对象中的参数 * @return * @throws Throwable */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object invoke = method.invoke(house, args); System.out.println("中介收取中介费"); return invoke; } }
package com.liubingfei.test.proxy.dynamicproxy.jdk_proxy; import org.junit.Test; /** * @author LiuBingFei * @desc * @date 2020/3/5 23:14 * @return */ public class Jdk_proxy { @Test public void proxy() { //传入被代理类,创建动态代理类的对象。 House_proxy house_proxy = new House_proxy(new House_owner()); //获得代理类,强转成接口类型。 House house = (House) house_proxy.getProxy(); //使用多态的向下造型 house.sell_house(); house.rental_house(); } }
标签:voc get org 方法 import instance row throws owa
原文地址:https://www.cnblogs.com/liubingfei/p/12424010.html