?? proxytest.java
字號:
import java.lang.reflect.*;
/**
* Description:
* <br/>Copyright (C), 2008-2010, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
interface Person
{
void walk();
void sayHello(String name);
}
class MyInvokationHandler implements InvocationHandler
{
/*
執行動態代理對象的所有方法時,都會被替換成執行如下的invoke方法
其中:
proxy-代表動態代理對象
method-代表正在執行的方法
args-代表執行代理對象方法時傳入的實參。
*/
public Object invoke(Object proxy, Method method, Object[] args)
{
System.out.println("正在執行的方法:" + method);
if (args != null)
{
System.out.println("下面是執行該方法時傳入的實參:");
for (Object val : args)
{
System.out.println(val);
}
}
else
{
System.out.println("調用該方法無需實參!");
}
return null;
}
}
public class ProxyTest
{
public static void main(String[] args)
throws Exception
{
//創建一個InvocationHandler對象
InvocationHandler handler = new MyInvokationHandler();
//使用指定的InvocationHandler來生成一個動態代理對象
Person p = (Person)Proxy.newProxyInstance(
Person.class.getClassLoader(),
new Class[]{Person.class}, handler);
//調用動態代理對象的walk()和sayHello()方法
p.walk();
p.sayHello("孫悟空");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -