/**
* 本程序主要用来练习静态代理的模式
* 也可为接口的练习题
* @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 发布
本文介绍了一个使用静态代理模式实现的简单示例。该示例通过定义一个结婚接口,并使用Person类和Agencycompany类来演示代理模式的应用。Person类实现结婚接口,而Agencycompany类作为代理类,负责处理结婚前后的准备工作。
316

被折叠的 条评论
为什么被折叠?



