亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 4623 | 回復(fù): 3
打印 上一主題 下一主題

假如有很多if else,為了體現(xiàn)面向?qū)ο螅檬裁磥?lái)代替呢? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2012-08-10 01:06 |只看該作者 |倒序?yàn)g覽
if(animal.IsCat()) {  }  
else if(animal.IsDog()) {  }
else if(animal.IsKoala()) {  }  
. . .  else if(A.isType(Type10)) { }
上面的代碼段中使用一個(gè)對(duì)象命名為“動(dòng)物!凹僭O(shè)有一個(gè)動(dòng)物的類(lèi)層次結(jié)構(gòu)。下面哪一項(xiàng)技術(shù)可以取代是以if - then - else構(gòu)造
1持久?
2Messaging
3抽象
4封裝
5多態(tài)

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2012-08-10 19:55 |只看該作者
  1. /**
  2. * File:Animal.java
  3. * ------------------
  4. * Polymorphism
  5. */
  6. package org.cudemo;

  7. /**
  8. * @author isaacxu
  9. *  *
  10. */
  11. public class Animal{
  12.         public void spark( ) {
  13.           
  14.           }

  15. }
復(fù)制代碼
  1. /**
  2. * File:Cat.java
  3. * -----------------
  4. * Cat objects are Animal
  5. * because they have the same interface:
  6. */
  7. package org.cudemo;

  8. public class Cat extends Animal{
  9.         // Redefine interface method
  10.         public void spark() {
  11.                 System.out.println("I am a Cat!");
  12.                   
  13.           }

  14. }
復(fù)制代碼
  1. /**
  2. * File:Dog.java
  3. * ---------------
  4. * Dog objects are Animal
  5. * because they have the same interface:
  6. */
  7. package org.cudemo;

  8. public class Dog extends Animal{
  9.         // Redefine interface method
  10.         public void spark( ) {
  11.                 System.out.println("I am a Dog!");   
  12.           }

  13. }
復(fù)制代碼
  1. /**
  2. * File:Koala.java
  3. * -------------------------
  4. * Koala objects are Animal
  5. * because they have the same interface:
  6. */
  7. package org.cudemo;

  8. /**
  9. * @author isaacxu
  10. *
  11. */
  12. public class Koala extends Animal{
  13.         // Redefine interface method
  14.         public void spark( ) {
  15.                 System.out.println("I am a Koala!");   
  16.           }

  17. }
復(fù)制代碼
  1. /**
  2. * File:Sound.java
  3. * ---------------
  4. * Test file like <Think in Java>:Inheritance & upcasting
  5. */
  6. package org.cudemo;

  7. public class Sound {

  8.         public static void barking(Animal a) {
  9.             // ...
  10.             a.spark();
  11.           }
  12.         public static void barkingAll(Animal[] all) {
  13.             for(Animal i : all)
  14.               barking(i);
  15.           }       
  16.         /**
  17.          * @param args
  18.          */
  19.         public static void main(String[] args) {
  20.                
  21.                 Animal[] pets = {
  22.                                 new Cat(),
  23.                                 new Dog(),
  24.                                 new Koala()
  25.                                 //.....
  26.                 };
  27.                 barkingAll(pets);

  28.         }

  29. }
復(fù)制代碼

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2012-08-11 11:32 |只看該作者
回復(fù) 2# isaacxu


    謝謝啊,例子很好
這個(gè)叫多態(tài)么?

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2012-08-13 18:48 |只看該作者
看你的代碼差不多是多態(tài),但還是要看具體操作了。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP