国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

靜態(tài)代理動(dòng)態(tài)代理以及aop

2019-11-11 01:12:22
字體:
供稿:網(wǎng)友

靜態(tài)代理和動(dòng)態(tài)代理

//靜態(tài)代理interface SubClass{	void action();}class SubClasss implements SubClass{	public void action(){		System.out.PRintln("執(zhí)行action");	}}class Proxy1 implements SubClass{	SubClasss s;	public Proxy1(SubClasss s){		this.s=s;	}	@Override	public void action() {		System.out.println("代理類執(zhí)行");		s.action();	}	}public class ProxyTest {	public static void main(String[] args) {		SubClass s = new Proxy1(new SubClasss());		s.action();		//動(dòng)態(tài)代理		Protest p = new Protest();		Object o =p.blind(s);		SubClass ss = (SubClass) o;		ss.action();		Childclass c = new Childclass();		ParentClass pa =(ParentClass) p.blind(c);		pa.action();	}}//動(dòng)態(tài)代理interface ParentClass{	void action();}class Childclass implements ParentClass{	@Override	public void action() {		System.out.println("執(zhí)行childclass的action方法");	}}class Protest implements InvocationHandler{	Object obj;	public Object blind(Object obj){		this.obj=obj;		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), 				obj.getClass().getInterfaces(), this);	}	@Override	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {		System.out.println("invoke方法");		Object returnVal = method.invoke(obj, args);		return returnVal;	}	}aop

interface Parent{	void fly();	void action();}class Child implements Parent{	@Override	public void fly() {		System.out.println("i believe i can fly");	}	@Override	public void action() {		System.out.println("執(zhí)行action");	}	}class ChildUtil{	void method1(){		System.out.println("=======方法一=======");	}	void method2(){		System.out.println("=======方法二=======");	}}class MyInvocation implements InvocationHandler{	Object obj;	void setObj(Object obj){		this.obj=obj;	}	@Override	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {		ChildUtil child = new ChildUtil();		child.method1();		Object o = method.invoke(obj, args);		child.method2();		return o;	}	}class Protest2{	public static Object getProxyInstance(Object obj){		MyInvocation m = new MyInvocation();		m.setObj(obj);		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), 				obj.getClass().getInterfaces(), m);					}}public class AopTest {	public static void main(String[] args) {		Child c = new Child();		Object obj = Protest2.getProxyInstance(c);		Parent p = (Parent) obj;		p.fly();		p.action();	}}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 科技| 鹤岗市| 宿迁市| 梅州市| 寿阳县| 五家渠市| 关岭| 右玉县| 保山市| 侯马市| 巴南区| 汉阴县| 察哈| 宁陵县| 徐汇区| 信阳市| 襄垣县| 扶沟县| 乡城县| 九江县| 平凉市| 和林格尔县| 会宁县| 巴林右旗| 揭东县| 肥乡县| 台前县| 昆明市| 哈巴河县| 东台市| 德兴市| 达日县| 东山县| 洪泽县| 西畴县| 清涧县| 成安县| 东乌| 兴仁县| 新巴尔虎左旗| 三明市|