【原創】Spring-AOP代理類繼承介面..
Computer.java
package org.rockie;
public class Computer implements PcInterf{
private String pcName="rockie007";
private int pcPrice=5000;
public String getPcName() {
return pcName;
}
public void setPcName(String pcName) {
this.pcName = pcName;
}
public int getPcPrice() {
return pcPrice;
}
public void setPcPrice(int pcPrice) {
this.pcPrice = pcPrice;
}
public void buy() {
// TODO 自動生成方法存根
System.out.println("獲得一部"+pcName+"電腦..");
}
}
PcInterf.java
package org.rockie;
public interface PcInterf {
public void buy();
}
Daili.java
package org.rockie;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Daili implements InvocationHandler{
private Object delegate;
public Object bind(Object delegate)
{
this.delegate=delegate;
//代理物件的類,代理物件的介面..
return Proxy.newProxyInstance(delegate.getClass().getClassLoader(), delegate.getClass().getInterfaces(), this);
}
public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable {
// TODO 自動生成方法存根
System.out.println("送滑鼠一個..");
Object result=arg1.invoke(delegate, arg2);
return result;
}
}
TestMain.java
package org.rockie;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成方法存根
Daili daili=new Daili();
PcInterf pc=(PcInterf)daili.bind(new Computer());
pc.buy();
}
}
ps:此帖絕對是本人原創...轉載時..
請註明:
轉至http://blog.csdn.net/Kipen/ 作者:rockie(石の頭丶)
請尊重他人的勞動果實..
謝謝..
--by rockie(石の頭丶)