【Boolan】第三周筆記

1.類與類之間的三種關(guān)系:1)Inheritance(繼承) 2)Composition(復(fù)合) 3)Delegation(委托)
2.Composition復(fù)合關(guān)系,可理解為has a

復(fù)合關(guān)系下的構(gòu)造函數(shù):
  從內(nèi)而外,Container的構(gòu)造函數(shù)先調(diào)用Component的default構(gòu)造函數(shù),再執(zhí)行自己
Container::Container(...):Component() {};
復(fù)合關(guān)系下的構(gòu)造函數(shù):
  從外而內(nèi),Container的析構(gòu)函數(shù)先執(zhí)行自己,然后再調(diào)用Component的析構(gòu)函數(shù)
Container::~Container(...){ ... ~Component() };

  PS:可理解為脫衣服跟穿衣服,構(gòu)造是穿衣服,先穿里面的再穿外面的,析構(gòu)是脫衣服,先脫外面的再穿外面的

3.Delegation委托關(guān)系:Composition by reference
4.Delegation繼承關(guān)系,可理解為is a
PS:如蘋(píng)果與水果的關(guān)系,蘋(píng)果是一種水果

繼承關(guān)系下的構(gòu)造函數(shù):
  從內(nèi)而外,Derived的構(gòu)造函數(shù)先調(diào)用Base的default構(gòu)造函數(shù),再執(zhí)行自己
  Derived::Derived(...):Base() {};
繼承關(guān)系下的構(gòu)造函數(shù):
  從外而內(nèi),Derived的析構(gòu)函數(shù)先執(zhí)行自己,然后再調(diào)用Base的析構(gòu)函數(shù)
  Derived::~Derived(...){ ... ~Base() };
  PS:與復(fù)合關(guān)系一樣

5.Inheritance with virtual function

not-virtual function:不希望derived class 重新去定義,保持這個(gè)函數(shù)不變
virtual function:derived class可以重新定義,但其有默認(rèn)值,所以也可以不重新定義
pure virtual function:derived class必須重新定義,因?yàn)椴淮嬖谀J(rèn)定義

6.Composite:Inheritance+Composition關(guān)系

繼承復(fù)合關(guān)系下的構(gòu)造函數(shù):
  從內(nèi)而外,Derived的構(gòu)造函數(shù)先調(diào)用Base的default構(gòu)造函數(shù),再執(zhí)行自己
  Derived::Derived(...):Base() , Component() {...};
繼承復(fù)合關(guān)系下的構(gòu)造函數(shù):
  從外而內(nèi),Derived的析構(gòu)函數(shù)先執(zhí)行自己,然后再調(diào)用Base的析構(gòu)函數(shù)
  Derived::~Derived(...){ ... ~Componet() , ~Base() };

7.創(chuàng)建未來(lái)才會(huì)出現(xiàn)的子類:Delegation+Inheritance(Prototype)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容