📄 dynamicproxy.java
字号:
package com.ntsky.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynamicProxy implements InvocationHandler{
private TestInterface testInterface;
public DynamicProxy(){
testInterface = new Test2();
}
public TestInterface create(){
TestInterface testInterface = null;
testInterface = (TestInterface)Proxy.newProxyInstance(TestInterface.class.getClassLoader(),new Class[]{TestInterface.class},this);
return testInterface;
}
public Object invoke(Object obj,Method method,Object[] args){
Object o = null;
try {
System.out.println("方法名="+method.getName());
if(method.getName().equals("sayHello")){
System.out.println("调用sayHello");
}else{
o = method.invoke(testInterface,args);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return o;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -