打字猴:1.700477609e+09
1700477609 //天鹅是能够飞行的
1700477610
1700477611 public void fly(){
1700477612
1700477613 System.out.println(“能够飞行”);
1700477614
1700477615 }
1700477616
1700477617 }
1700477618
1700477619 但是,鸭妈妈却不认为自己这个另类的孩子是白天鹅,它从自己的观点出发,认为她很丑陋,有碍自己的脸面,于是驱赶她——鸭妈妈把这只小天鹅误认为一只鸭。我们来看实现,如代码清单31-16所示。
1700477620
1700477621 代码清单31-16 把白天鹅当做小鸭子看待
1700477622
1700477623 public class UglyDuckling extends WhiteSwan implements Duck{
1700477624
1700477625 //丑小鸭的叫声
1700477626
1700477627 public void cry(){
1700477628
1700477629 super.cry();
1700477630
1700477631 }
1700477632
1700477633 //丑小鸭的外形
1700477634
1700477635 public void desAppearance(){
1700477636
1700477637 super.desAppaearance();
1700477638
1700477639 }
1700477640
1700477641 //丑小鸭的其他行为
1700477642
1700477643 public void desBehavior(){
1700477644
1700477645 //丑小鸭不仅会游泳
1700477646
1700477647 System.out.println(“会游泳”);
1700477648
1700477649 //还会飞行
1700477650
1700477651 super.fly();
1700477652
1700477653 }
1700477654
1700477655 }
1700477656
1700477657 天鹅被看成了鸭子,有点暴殄天物的感觉。我们再来创建一个场景类来描述这一场景,如代码清单31-17所示。
1700477658
[ 上一页 ]  [ :1.700477609e+09 ]  [ 下一页 ]