1 /* 2 不同地方饮食文化不同案例 3 */ 4 class Person{ 5 public void eat(){ 6 System.out.println("吃饭"); 7 } 8 } 9 10 class SouthPerson extends Person { 11 public void eat(){ 12 System.out.println("吃米饭"); 13 } 14 public void sell(){ 15 System.out.println("经商"); 16 } 17 } 18 19 class NorthPerson extends Person { 20 public void eat(){ 21 System.out.println("吃馒头"); 22 } 23 public void study(){ 24 System.out.println("研究"); 25 } 26 } 27 28 class DuoTaiTest{ 29 public static void main(String[] args){ 30 Person p = new SouthPerson(); 31 p.eat(); 32 System.out.println("----------"); 33 34 SouthPerson sp = (SouthPerson) p; 35 sp.eat(); 36 sp.sell(); 37 System.out.println("----------"); 38 39 p = new NorthPerson(); 40 p.eat(); 41 System.out.println("----------"); 42 43 NorthPerson np = (NorthPerson) p; 44 np.eat(); 45 np.study(); 46 } 47 48 }
运行结果:
吃米饭 ---------- 吃米饭 经商 ---------- 吃馒头 ---------- 吃馒头 研究
时间: 2024-11-09 00:48:57