C_and_CPP 板


LINE

※ 引述《phdch (我們都活在這個城市裡面)》之銘言: : 下面程式碼 : class Apple:public Fruit : 中的修飾詞public : 我知道是存取權限多少的關係 : class中的public成員,private成員,我理解 : 但public的類別(Fruit)要怎麼解釋與子類別(Apple)的關係最好呢??? : 感謝 簡單的說法是「Apple 是一種 Fruit」 (Apple is-a Fruit) 但其實這樣的說法可能會引人誤解, 因為在 OOP 中,「is-a」和一般人對「是一個...」或「屬於...」的認知有一點不同 在 OOP 中,Apple is-a Fruit 意指所有對父類別 (Fruit) 的操作,都可以 運用在子類別 (Apple) 身上。比如說 Fruit 提供了 taste() 這個成員函式, 可以傳回該水果嘗起來的味道,那麼 Apple 也有義務提供 taste() 這個函 式,顯示出蘋果的味道如何。 class Fruit { public: virtual string taste() = 0; }; class Apple : public Fruit { public: virtual string taste() { return "Apple is sweat."; } }; 這樣的好處是,對於不同種類的水果,我們都可以用相同的操作去顯示它的味道: class Lemon : public Fruit { public: virtual string taste() { return "Lemon is sour."; // 檸檬很酸 } }; class ArtificialFruit : public Fruit { // 某種人造水果 public: virtual string taste() { string result; ... // 進行某些複雜的運算來得到它的味道 return result; } }; void print_taste(Fruit& fruit) { // 我不知道 fruit 是什麼水果 // 但我知道,只要是 Fruit,就可以用 taste() 取得它的味道 cout << fruit.taste() << endl; } int main() { Apple apple; Lemon lemon; ArtificialFruit unknow; // apple is-a Fruit // Fruit 提供 taste() 來取得水果的味道 // 所以 apple 也可以用 taste() 來取得味道 print_taste(apple); print_taste(lemon); // 同上,印出檸檬的味道 print_taste(unknow); // 同上,印出某種未知的味道 return 0; } 不管你發明怎樣的水果,只要這種水果提供 taste() 界面,那你不需要修改 既有的 print_taste 程式碼,就可以印出該水果的味道。這正是 OOP 中最 基本的概念。 至於 private inheritance,在概念上和 public inheritance 有極大的不同, 它並不會形成 is-a 的關係,而比較類似 has-a。如果你對這些議題有興趣, 建議你可以看看以下的參考資料: * Effective C++ 條款 35:確定你的 public inheritance 模塑出 is-a 的關係 條款 36:區分介面繼承及實作繼承 條款 42:明智地運用 private inheritance * Exceptional C++ 條款 22:class 設計的常見錯誤 --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 219.87.151.2
1F:推 VictorTom:推:) 小弟今天負責來推文的就對了XD 05/08 12:38
2F:推 Peruheru:這篇文章真有趣 05/08 12:38
3F:推 akasan:推~不過到現在還沒實際用過public繼承以外XD 05/08 15:37
4F:→ tinlans:關鍵字:LSP 05/08 21:50
5F:推 softwind:好文(好聞) 推一個 05/08 22:29







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP