編輯:關於 Android 程式設計
多重繼承是 C++ 裡威力很強、也很容易被誤用的特性。下面這個經典範例——「既是教師、又是幹部」的人——剛好把它的要點與陷阱都涵蓋了:如何同時繼承兩個基類、如何在建構子中依序初始化、以及當兩個基類有同名成員時該怎麼消歧義。
/*
* Copyright (c) 2013, 煙台大學計算機學院
* All rights reserved.
* 檔本名稱:test.cpp
* 作者:樊露露
* 完成日期:2013 年 5 月 27 日
* 版本號:v1.0
*/
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
public:
Teacher(string n, int a, char s, string ad, string t, string ti);
void display();
private:
string name;
int age;
char sex;
string add;
string tel;
string title; // 職稱
};
Teacher::Teacher(string n, int a, char s, string ad, string t, string ti)
: name(n), age(a), sex(s), add(ad), tel(t), title(ti) { }
void Teacher::display()
{
cout << "name:" << name << endl;
cout << "age:" << age << endl;
cout << "sex:" << sex << endl;
cout << "title:" << title << endl;
cout << "address:" << add << endl;
cout << "tel:" << tel << endl;
}
Teacher 用初始化列表完成所有成員的賦值,這是 C++ 較推薦的寫法,對於 const 成員與參考成員更是唯一選擇。
class Cadre
{
public:
Cadre(string n, int a, char s, string ad, string t, string p);
void display();
private:
string name;
int age;
char sex;
string add;
string tel;
string post; // 職務
};
Cadre::Cadre(string n, int a, char s, string ad, string t, string p)
{
name = n;
age = a;
sex = s;
add = ad;
tel = t;
post = p;
}
void Cadre::display()
{
cout << "name:" << name << endl;
cout << "age" << age << endl;
cout << "sex:" << sex << endl;
cout << "post:" << post << endl;
cout << "address:" << add << endl;
cout << "tel:" << tel << endl;
}
這裡刻意用另一種寫法(在建構子body中逐一賦值)作對照。兩者結果相同,但前者更簡潔。
class Teacher_Cadre : public Teacher, public Cadre
{
public:
Teacher_Cadre(string n, int a, char s, string ad, string t,
string ti, string p, int w);
void show();
private:
string name;
int age;
char sex;
string add;
string tel;
int wages;
};
Teacher_Cadre::Teacher_Cadre(string n, int a, char s, string ad, string t,
string ti, string p, int w)
: Teacher(n, a, s, ad, t, ti), Cadre(n, a, s, ad, t, p), wages(w) { }
void Teacher_Cadre::show()
{
Teacher::display();
// cout << "post:" << Cadre::post << endl; // post 是 private,無法直接存取
cout << "wages:" << wages << endl;
}
這段程式碼裡有兩個關鍵細節值得說明。
建構子的初始化順序。派生類建構時,會依「類別宣告順序」而非「初始化列表的書寫順序」呼叫各基類建構子,因此 Teacher 一定先於 Cadre 被建構,最後才初始化自己的 wages。
同名成員的消歧義。Teacher 與 Cadre 各自都有 name、age、sex、add、tel 與 display()。如果直接寫 te_ca.name,編譯器無從判斷你指的是哪一份,會報 ambiguous 錯誤;解法就是加上作用域解析運算子,寫成 Teacher::name 或 Cadre::name。show() 中呼叫 Teacher::display() 正是這個道理。至於被註解掉的那行,是因為 post 屬於 Cadre 的 private 成員,即使寫成 Cadre::post 也依然存取不到——這與歧義問題是兩回事。
另外可以注意到,Teacher_Cadre 自己也重新宣告了一份 name、age 等成員,這些會把基類的同名成員「遮蔽」起來,通常是不必要的設計,實務上建議移除,改用基類的 protected 成員或提供存取器。
int main()
{
Teacher_Cadre te_ca("Wang-li", 50, 'f',
"135 Beijing Road,Shanghai",
"(021)61234567",
"president", "prof.", 1534);
te_ca.show();
return 0;
}
注意建構子實參數目與順序:第 6 個參數 "president" 會傳給 Teacher 的 title,第 7 個 "prof." 則傳給 Cadre 的 post。撰寫這類多參數建構子時,強烈建議改用具名的設定方式或引入 setter,單靠位置傳值非常容易出錯。
多重繼承在 C++ 中之所以名聲不佳,是因為它會帶來「菱形繼承」問題:若 Teacher 與 Cadre 都繼承自同一個 Person,那麼 Teacher_Cadre 中就會存在兩份 Person 子物件,需要以 virtual 繼承化解。因此實務上的建議是:優先考慮組合(把 Teacher 與 Cadre 作為成員變數),或改用介面(純虛擬基類)來表達「能同時具備多種身分」的概念。只有在這些能力真的需要共用同一份實作、且繼承層次清楚的情況下,多重繼承才是合適的工具。
Android 基礎入門教學:13 種 Drawable 圖解 Part 1
本節引言 從這節開始進入繪圖與動畫的基礎,目的是替後面的自訂 View 打地基。第一課先把 Drawable 這塊地翻一遍——Android 一共提供了 13 種 Dra
Android 五種資料儲存方式實戰(上):檔案、SharedPreferences 與 SQLite 完整範例
Android 平台提供了五種主要的資料儲存形式:檔案儲存、SharedPreferences 儲存、資料庫儲存、ContentProvider 內容提供者,以及網路儲存
Android TextView 實作:讓文字與表情圖片混排顯示
本文分享如何在 Android 中讓 TextView 同時顯示表情圖片與文字,供需要的朋友參考。 從這個案例可以學到:在美化介面時,我們可以在同一個區域讓圖片與文字混排
Android 面試備考:JUnit 單元測試入門
什麼是 JUnit JUnit 是 Java 語言的單元測試框架,屬於白盒測試的一種。傳統上若要驗證某個方法的結果,得自行撰寫一個 main 入口去呼叫它;而 JUnit