?????????????????
???????????? ???????[ 2013/8/7 11:04:16 ] ????????
???????????????fly()??????quack()????????Duck.java?????????????????????á???????????????????μ???????????????е????????????Duck.java??????????????и??????????????????????????????????????????????????γ???(????Squeak.java)????OO?У???????"????"??????????(????????????з???????????????????"????"?????????????????????????????????????????????????Щ?????????е?????????и????????
????2)????仯???????????????
????Duck.java??fly()???quack()???????и?????????
??????????????
publicabstractclassDuck{
//????????????????????????????????????????
FlyBehaviorflyBehavior;
QuackBehaviorquackBehavior;
publicvoidperformFly(){
//?????д???fly()?????????????????flyBehavior?????????????
flyBehavior.fly();
}
publicvoidperformQuack(){
quackBehavior.quack();
}
publicvoidswim(){
System.out.println("Allducksfloat??evendecoys.");
}
publicabstractvoiddisplay();
}
Duck.java????????ν???fly()???quack()????Щ?????????????????ɡ?
publicclassMallardDuckextendsDuck{
publicMallardDuck(){
flyBehavior=newFlyWithWings();
quackBehavior=newQuack();
}
publicvoiddisplay(){
System.out.println("Greenhead.");
}
}
??????
publicclassDuckTest{
publicstaticvoidmain(String[]args){
Duckduck=newMallardDuck();
duck.performFly();
duck.performQuack();
}
}
??????Duck.java??????MallardDuck.java????????У???????????????????????????????????????????????????????
????1)????????????????????????????????(??ο???????????);
????2)?????????????????????????
????3)????趨???
?????????Duck.java???????趨????????setter?????????????????????????????????????????????????????????????????????????????????????????????????????????
publicabstractclassDuck{
//????Duck.java?м????????????????
publicvoidsetFlyBehavior(FlyBehaviorflyBehavior){
this.flyBehavior=flyBehavior;
}
publicvoidsetQuackBehavior(QuackBehaviorquackBehavior){
this.quackBehavior=quackBehavior;
}
//??????????????...
}
??????????
publicclassDuckTest{
publicstaticvoidmain(String[]args){
Duckduck=newMallardDuck();
duck.performFly();
duck.performQuack();
duck.setFlyBehavior(newFlyNoWay());
duck.performFly();
}
}
??????????????????????????????????????????FlyBehavior.java????????????????????????????setQuackBehavior(...)?????????????????Duck.java?????μ???????????????????????????????????
???????????????????????????????и???????????????????????ζ???????:
????3.???????
????MallardDuck???Duck??????->?????????
????FlyWithWings???FlyBehavior????->?仯??????????????
??????Duck.java??setter?????????????->????趨???
????????????????????????????????????????????????迴????:
????1)?????????????????????????????????????????????->??У?isa
????2)??????????????????????????????????????????????????????->????hasa
????setter?????????????
??????У????????????????????????????????????????????????????У????????????????????????????????仯??
???????????????????ü?У?????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11