亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

jdk動態代理實現作業怎么寫

作業怎么實現有同時記錄時間和日子?

正在回答

5 回答

package?com.jdkproxy;

import?java.lang.reflect.InvocationHandler;
import?java.lang.reflect.Method;

/*
?*?日志
?*/
public?class?LogHandler?implements?InvocationHandler?{
	
	private?Object?target;

	public?LogHandler?(Object?target){
		super();
		this.target?=?target;
	}

	public?Object?getTarget()?{
		return?target;
	}

	public?void?setTarget(Object?target)?{
		this.target?=?target;
	}

	@Override
	public?Object?invoke(Object?proxy,?Method?method,?Object[]?args)
			throws?Throwable?{
		System.out.println("日志記錄開始。。。");
		method.invoke(target);
		System.out.println("日志記錄結束。。。");
		return?null;
	}

}


0 回復 有任何疑惑可以回復我~
package?com.jdkproxy;

import?java.lang.reflect.InvocationHandler;
import?java.lang.reflect.Proxy;

import?com.proxy.Car;
import?com.proxy.Moveable;

public?class?Text?{

	/**
	?*?JDK動態代理測試類
	?*?
	?*?動態代理實現步驟
	?*?1.創建一個實現InvocationHandler接口的類,它必須實現invoke方法
	?*?2.創建被代理的類和接口
	?*?3.調用Proxy的靜態方法,創建一個代理類newProxyInstance()
	?*?4.通過代理調用方法
	?*?
	?*/
	public?static?void?main(String[]?args)?{

		Car?car?=?new?Car();
		InvocationHandler?h?=?new?TimeHandler(car);
		Class<?>?cls=?car.getClass();
		/*
		?*?loader?類加載器
		?*?interfaces?實現接口
		?*?h?InvocationHandler
		?*/
		Moveable?m?=?(Moveable)Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?h);
		
		//同時實現時間和日志的動態代理
		InvocationHandler?l?=?new?LogHandler(m);
		Moveable?m1?=?(Moveable)Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?l);
		m1.move();
	}

}

package com.jdkproxy;


import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;


/*

?* 日志

?*/

public class LogHandler implements InvocationHandler {

private Object target;


public LogHandler (Object target){

super();

this.target = target;

}


public Object getTarget() {

return target;

}


public void setTarget(Object target) {

this.target = target;

}


@Override

public Object invoke(Object proxy, Method method, Object[] args)

throws Throwable {

System.out.println("日志記錄開始。。。");

method.invoke(target);

System.out.println("日志記錄結束。。。");

return null;

}


}


0 回復 有任何疑惑可以回復我~
public?class?Test?{
	public?static?void?main(String[]?args)?{
		Car?car?=?new?Car();
		//timeHandler是添加了time的car代理
		InvocationHandler?timeHandler=??new?TimeProxy(car);
		Class<?>?cls?=?car.getClass();
		
		Move?timemove?=?(Move)?Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?timeHandler);
		//注意此處timemove代替car,logHandler則為添加了time和log的car代理
		InvocationHandler?logHandler?=??new?LogProxy(timemove);
		
		/**
		?*?loader:類加載器,
		?*?interfaces:實現接口
		?*?h:InvocationHandler
		?*/
		
		Move?logmove?=?(Move)?Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?logHandler);
		logmove.move();
	}
}


1 回復 有任何疑惑可以回復我~
public?static?void?main(String[]?args)?{
		Car?car?=?new?Car();
		InvocationHandler?h?=??new?JDKProxy(car);
		Class<?>?cls?=?car.getClass();
		Move?m?=?(Move)?Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?h);
		InvocationHandler?h1?=??new?LogProxy(m);
		
		/**
		?*?loader:類加載器,
		?*?interfaces:實現接口
		?*?h:InvocationHandler
		?*/
		
		Move?m1?=?(Move)?Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?h1);
		m1.move();
	}


1 回復 有任何疑惑可以回復我~

把相關信息都加一個里面

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

jdk動態代理實現作業怎么寫

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號