大家提供自己的参考下,看看大家如何修改的。 根据提供的主程序修改程序,定义不同的行为类。 主程序中Say and Sing 不是String。比如 say 和 sing 各定义为类。看看大家发挥,
根据这个主程序:谢谢
.....
Person father = new Father();
father.setContent("How are you");
father.act(sing); Mother mother = new Mother();
mother.act(say);
....
修改如下程序:
public class PloyTest1
{
public void say(Person person)
{
person.say();
}
public void sing(Person person)
{
person.sing();
}
public static void main(String[] args)
{
PloyTest1 test = new PloyTest1();
Person person = new Father();
// test.say(person);
test.sing(person);
Mother mother = new Mother();
test.say(mother);
}
}
//class Person
abstract class Person
{
/*public void say()
{
System.out.println("person is saying");
}*/
/*public void sing()
{
// TODO Auto-generated method stub
System.out.println("");
}*/
public abstract void say();
public void sing(){}
}
class Father extends Person
{
public void say()
{
System.out.println("father is saying: How are you?");
}
@Override
public void sing()
{
// TODO Auto-generated method stub
System.out.println("father sing: how are you");
}
}
class Mother extends Person
{
public void say()
{
System.out.println("mother is saying: I am fine");
}
/*@Override
public void sing()
{
// TODO Auto-generated method stub
}*/
}
根据这个主程序:谢谢
.....
Person father = new Father();
father.setContent("How are you");
father.act(sing); Mother mother = new Mother();
mother.act(say);
....
修改如下程序:
public class PloyTest1
{
public void say(Person person)
{
person.say();
}
public void sing(Person person)
{
person.sing();
}
public static void main(String[] args)
{
PloyTest1 test = new PloyTest1();
Person person = new Father();
// test.say(person);
test.sing(person);
Mother mother = new Mother();
test.say(mother);
}
}
//class Person
abstract class Person
{
/*public void say()
{
System.out.println("person is saying");
}*/
/*public void sing()
{
// TODO Auto-generated method stub
System.out.println("");
}*/
public abstract void say();
public void sing(){}
}
class Father extends Person
{
public void say()
{
System.out.println("father is saying: How are you?");
}
@Override
public void sing()
{
// TODO Auto-generated method stub
System.out.println("father sing: how are you");
}
}
class Mother extends Person
{
public void say()
{
System.out.println("mother is saying: I am fine");
}
/*@Override
public void sing()
{
// TODO Auto-generated method stub
}*/
}
解决方案 »
- 菜鸟又来发问
- 高分求JSF的表格解决方案!!
- java 使用registry-3.1.3读取注册表 字符串中文的乱码问题?不知道如何解决
- 请问:think in java 与 java 核心技术I、II 应该选哪一本呢?
- 关于Jmster 测试webservice
- JList问题
- 问一个关于位运算的问题
- 一个小小的问题,请大家看看!!!
- 我和我的朋友机器联网了,我的我的java程序(用jdbc驱动)可以连到他的sqlserver.但不可以连到我自己的。但我的java程序(用jdbc-odbc桥)连
- 求助!为什么子类方法不会abstract类的普通方法
- 这道java基础题,哪位高手能够指点下,谢谢
- 一道java题,请高手指点
如果sing和say是对象,比如这段主程序之前定义 Sing sing = new Sing()之类的,那么很显然,act方法接收的参数就是sing和say的共同父类或超类或接口所以
可以采用接口
interface IMouthAction {
void action();
}class Sing implement IMouthAction {
public void action() {System.out.println("sing");}
}class Say implement IMouthAction {
public void action() {System.out.println("say");}
}abstract class Person {
public void act(IMouthAction action) {action.action();}
}class Father extends Person {}
class Mather extends Person {}public class Main {
public static void main(String[] args) {
IMouthAction sing = new Sing();
IMouthAction say = new Say(); ... //LZ的主代码部分
}
}