静态代理中:目标对象和代理对象要实现同一个接口,实现灵活的切换对象
测试类:
public class Test1 {
public static void main(String[] args) {
Server agent=new agent(new Goal());
agent.show();
}
}
接口:
public interface Server {
public void show();
}
代理类:
public class agent implements Server {
Server agent ;
public agent(Goal goal) {
}
public void getagent(Server agent){
this.agent=agent;
}
@Override
public void show() {
System.out.println("呼叫目标函数!!!!");
agent.show();
}
}
目标函数:
public class Goal implements Server{
@Override
public void show() {
System.out.println("目标函数正在运行......");
}
}