import java.lang.reflect.*;
import java.util.*;
public class proxy {
public static void main(String[] args) {
String s ="Happy";
InvocationHandler handler = new Handler(s);
Class[] interfaces = s.getClass().getInterfaces();
Object myproxy = Proxy.newProxyInstance(null,interfaces,handler);
System.out.println(myproxy.compareTo("hoppu"));
}
}
class Handler implements InvocationHandler {
public Handler(Object t) {
target = t;
}
public Object invoke(Object proxy,Method m,Object[] args) throws Throwable {
System.out.println(m.getName());
return m.invoke(target,args);
}
private Object target;
}
代理对象可以在实现接口时调用接口。编译此代码时出现此错误。
proxy.java:19: cannot find symbol
symbol : method compareTo(java.lang.String)
location: class java.lang.Object
System.out.println(proxy.compareTo("hoppu"));
^
1 error
我也尝试过 Integer ...同样的错误。