62,634
社区成员




public interface Human
{
//获得性别
String getSex();
//获得说话内容
String getWords();
}
这样就ok了,给第一题用的时候可以这样:
Human h = new ....();//Boy or Girl
System.out.println(h.getSex()+", to say:"+h.getWords());
public interface Human
{
//获得性别
String getSex();
//获得说话内容
String sayHello();
}
public class Boy
implements Human
{
public String sayHello()
{
return "Hello";
}
public String getSex()
{
return "Boy";
}
}
public class Girl
implements Human
{
public String sayHello()
{
return "World";
}
public String getSex()
{
return "Girl";
}
}
public interface Say
{
void say();
}
/**
* 说 的代理类
*/
public class SayProxy
implements Say
{
private Human human;
public SayProxy(Human human)
{
this.human = human;
}
public void say()
{
System.out.println(human.getSex() + ", to say:" + human.sayHello());
}
}
public interface Sing
{
void sing();
}
/**
* 唱 的代理类
*/
public class SingProxy
implements Sing
{
private Human human;
public SingProxy(Human human)
{
this.human = human;
}
public void sing()
{
System.out.println(human.getSex() + ", to sing:" + human.sayHello());
}
}
public class TestProxy
{
public static void main(String[] args)
{
//男孩女孩定义
Human boy = new Boy();
Human girl = new Girl();
//男孩说
Say boySay = new SayProxy(boy);
boySay.say();
//女孩唱
Sing girlSing = new SingProxy(girl);
girlSing.sing();
//女孩说
Say girlSay = new SayProxy(girl);
girlSay.say();
//男孩唱
Sing boySing = new SingProxy(boy);
boySing.sing();
}
}