日期:2014-05-20 浏览次数:20913 次
.....
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
}*/
}
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的主代码部分
}
}