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)