接口代码
public interface ITest {
int add(int i , int j);
int jian(int i , int j);
}
实现类代码
public class TestImpl implements ITest {
@Override
public int add(int i, int j) {
return i + j;
}
@Override
public int jian(int i, int j) {
return i - j;
}
}
代理代码
public class Test {
public static void main(String[] args) {
TestImpl impl =new TestImpl();
ClassLoader classLoader = impl.getClass().getClassLoader();
Class<?>[] interfaces = impl.getClass().getInterfaces();
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(method.getName() + "executed!");
Object invoke = method.invoke(impl, args);
System.out.println("args: " + args.toString());
return invoke;
}
};
ITest object = (ITest) Proxy.newProxyInstance(classLoader, interfaces, handler);
int add = object.add(1, 2);
int jian = object.jian(4, 2);
}
}
735



被折叠的 条评论
为什么被折叠?



