1700477559
鸭类有3个行为,一个是鸭会叫,一个是外形描述,还有一个是综合性的其他行为描述,例如会游泳等。我们来看鸭妈妈的4个正宗孩子,如代码31-14所示。
1700477560
1700477561
代码清单31-14 小鸭子
1700477562
1700477563
public class Duckling implements Duck{
1700477564
1700477565
public void cry(){
1700477566
1700477567
System.out.println(“叫声是嘎——嘎——嘎”);
1700477568
1700477569
}
1700477570
1700477571
public void desAppearance(){
1700477572
1700477573
System.out.println(“外形是黄白相间,嘴长”);
1700477574
1700477575
}
1700477576
1700477577
//鸭子的其他行为,如游泳
1700477578
1700477579
public void desBehavior(){
1700477580
1700477581
System.out.println(“会游泳”);
1700477582
1700477583
}
1700477584
1700477585
}
1700477586
1700477587
4只正宗的小鸭子形象已经清晰地定义出来了。鸭妈妈还有一个孩子,就是另类的丑小鸭,她实际是一只白天鹅。我们先定义出白天鹅,如代码清单31-15所示。
1700477588
1700477589
代码清单31-15 白天鹅
1700477590
1700477591
public class WhiteSwan implements Swan{
1700477592
1700477593
//白天鹅的叫声
1700477594
1700477595
public void cry(){
1700477596
1700477597
System.out.println(“叫声是克噜——克噜——克噜”);
1700477598
1700477599
}
1700477600
1700477601
//白天鹅的外形
1700477602
1700477603
public void desAppaearance(){
1700477604
1700477605
System.out.println(“外形是纯白色,惹人喜爱”);
1700477606
1700477607
}
1700477608
[
上一页 ]
[ :1.700477559e+09 ]
[
下一页 ]