62,614
社区成员
发帖
与我相关
我的任务
分享
public interface Computer {
public void buy();
}
public class ComputerImpl implements Computer {
public void buy() {
System.out.println("给啥要啥");
}
}
public class DaiLi implements InvocationHandler {
Object obj;
public Object bind(Object obj){
this.obj = obj;
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this); //这里返回的是什么呢?里面的三个参数被代理对象,还是本类的这个对象呢
}
public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable { //这个方法什么时候执行的呢,并没有哪个对象调用它啊,里面的三个参数代表什么意思呢 System.out.println("您想要啥"); //没被调用为什么会输出呢
Object result = arg1.invoke(obj, arg2); //这个方法什么时候执行的呢,也没有对象调用它啊,arg1,arg2又是什么意思呢 return result;
}
}
public class Demo {
public static void main(String[] args) {
DaiLi dl = new DaiLi();
Computer c = (Computer)dl.bind(new ComputerImpl());
c.buy();
}
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object obj = null;
if(!method.getName().equals("close")) {
// 不是调用close方法时,继续做原本的事情
obj = method.invoke(con, args);
}else{
// 是close方法时,将连接归还到池中
}
return obj;
}