/**
* 本程序主要用来练习静态代理的模式
* 也可为接口的练习题
* @author lzd
*
*/
public class Staticproxy {
public static void main(String[] args) {
Person man = new Staticproxy().new Person("lzd");
man.marry();
Agencycompany agencycompany = new Staticproxy().new Agencycompany(man);
//这里需要注意不能直接创建,原因是子类是动态的
//必须先在父类后才能引用创建子类,否则报错
agencycompany.marry();
}
/**
* 结婚接口,
* @author lzd
*
*/
interface Marry
{
void marry();
}
/**
* 结婚主
* @author lzd
*
*/
class Person implements Marry
{
private String name;
public Person(String name) {
this.name = name;
}
public String toString()
{
return name;
}
@Override
public void marry() {
System.out.println("I will get married with a woman");
}
}
/**
* 婚庆公司
* @author lzd
*
*/
class Agencycompany implements Marry
{
Person man;
public Agencycompany(Person man) {
this.man = man;
}
public void before() {
System.out.println("make compare for them");
}
public void after() {
System.out.println("get money from them");
}
@Override
public void marry() {
before();
System.out.println("Running in marry for "+man.toString());
after();
}
}
}
静态代理模式(java),接口练习
最新推荐文章于 2021-10-07 19:50:07 发布