1,能否利用面向对象的多态的概念,做一个模拟两个小朋友说话的简单程序:男同学说“Hello”,女同学说“World”。(也就是,输出 “Boy, to say: Hello")。主程序请简单依次生成对象,放进一个数据结构,然后简单循环完成行为;设计的重点应该放在类之间的关系和不同类的知识的分配。我们只是要求简单指示,文本输出;请不要在通讯细节或者对话回合上复杂化了。也不需要Proxy,InvocationHandler,Reflection 等手法。2,在这个版本的基础上:
- 保留现有的关于人的各个类
- 另外建立一个独立的负责行为(say)的类,把人类的实例作为代理
- 人类集中管理对话的内容和自己的称谓,不直接参与输出;行为类负责动作的描述和输出。3,在这个版本的基础上,如果我们假定不只是有“说”这个概念, 还增加了“唱”的概念。(说唱是概念上的而已,输出上还是println “boy to sing: hello" “girl to say: world" 等等,就可以了)而我们是可以设定不同的人用不同的行为来表达的。 重要一点,我们希望把这两种行为依然是作为类各自封装起来。在新的版本的主程序中,我们直接设定动作和人物就可以了,(不必要用循环语句了),例如,要男孩说,而女孩唱,或者女孩说,男孩唱。(三个题目是有关联的)
- 保留现有的关于人的各个类
- 另外建立一个独立的负责行为(say)的类,把人类的实例作为代理
- 人类集中管理对话的内容和自己的称谓,不直接参与输出;行为类负责动作的描述和输出。3,在这个版本的基础上,如果我们假定不只是有“说”这个概念, 还增加了“唱”的概念。(说唱是概念上的而已,输出上还是println “boy to sing: hello" “girl to say: world" 等等,就可以了)而我们是可以设定不同的人用不同的行为来表达的。 重要一点,我们希望把这两种行为依然是作为类各自封装起来。在新的版本的主程序中,我们直接设定动作和人物就可以了,(不必要用循环语句了),例如,要男孩说,而女孩唱,或者女孩说,男孩唱。(三个题目是有关联的)
思路:男女可以继承一个抽象类,各自实现Say的方法,同时也建立一个getWord,getGender方法,
类似于Say() { System.out.println(getWord()); } 2. 有点象策略模式的变形
Action类接收男女的具体类,然后根据具体类的word和gender控制输出3. 第三个象是桥接模式和策略模式的综合,关键是接收两个参数(男女具体类和行为),用多态
但是可以比较灵活实现楼上装饰模式应该是不太像,装饰模式应该是男孩既能唱,又能跳,强调的是不改变原功能的情况下做功能的叠加
private Action action; public String getWord() {
return word;
} public void setWord(String word) {
this.word = word;
} public Action getAction() {
return action;
} public void setAction(Action action) {
this.action = action;
}}
public class Girl { private String word;
private Action action; public String getWord() {
return word;
} public void setWord(String word) {
this.word = word;
} public Action getAction() {
return action;
} public void setAction(Action action) {
this.action = action;
}}
public interface Action {
void doSomething(String content);
}
public class Say implements Action{ public void doSomething(String content) {
// TODO Auto-generated method stub
System.out.println("this action is say :"+content);
}}
public class Sing implements Action{ public void doSomething(String content) {
// TODO Auto-generated method stub
System.out.println("this action is sing :"+content);
}
}
public static void main(String[] args) {
Boy boy=new Boy();
boy.setWord("boy, say hello");
Girl gril=new Girl();
gril.setWord("girl,sing world");
Action sayAction=new Say();
Action singAction=new Sing();
boy.setAction(sayAction);
gril.setAction(singAction);
boy.getAction().doSomething(boy.getWord());
gril.getAction().doSomething(gril.getWord());
}
{
//获得性别
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();
}
}
Human 人类不能有动作sayHello吧? 看看题目
public interface Human
{
//获得性别
String getSex();
//获得说话内容
String getWords();
}这样就ok了,给第一题用的时候可以这样:
Human h = new ....();//Boy or Girl
System.out.println(h.getSex()+", to say:"+h.getWords());