小菜在簡單學習原始碼過程中經常遇到 mixin 型別的 Class 類,而小菜之前是做 Android 開發的,Java / Kotlin 中並沒有 mixin 的概念,小菜今天簡單瞭解一下;
Mixin
基本介紹
Mixin 是一種在多個類層次結構中重用類程式碼的方法;小菜查閱了很多資料,比較官方的介紹是:
Mixin 是物件導向程式設計語言中的類,提供了方法的實現,其他類可以訪問 Mixin 類的方法而不必成為其子類;Mixin 為使用它的 Class 類提供額外的功能,但自身卻不單獨使用(不能單獨生成例項物件,屬於抽象類),Mixin 類通常作為功能模組使用,在需要該功能時“混入”,而且不會使類的關係變得複雜; Mixin 有利於程式碼複用性同時又避免了多繼承的複雜性,使用 Mixin 享有單一繼承的單純性和多重繼承的共有性,interface 介面與 Mixin 相同的地方是都可以多繼承,不同的地方在於 Mixin 是可以實現的;
對應關係
繼承 | 混入 | 介面 |
---|---|---|
關鍵字 | extends | with |
對應數量 | 1:1 | 1:n |
程式碼設定順序 | 前 | 中 |
案例嘗試
小菜嘗試最簡單的案例來逐步學習 Mixin;小菜分別定義了 People / Teacher / Student 三個普通的 Class,實現一個基本的 speak() 方法;
class People { void speak() { print('People --> speak'); } }
class Student { void speak() { print('Student --> speak'); } }
class Teacher { void speak() { print('Teacher --> speak'); } }
// mixin Teacher { void speak() { print('Teacher --> speak'); } }
複製程式碼
1. extends 繼承
class People01 extends Student { }
class People02 extends Teacher { }
class People03 extends Teacher { void speak() { print('People03 --> speak'); } }
print('<----------- People01 ----------->');
People01 people01 = People01(); people01.speak();
print('<----------- People02 ----------->');
People02 people02 = People02(); people02.speak();
print('<----------- People03 ----------->');
People03 people03 = People03(); people03.speak();
複製程式碼
使用繼承時需要使用 extends 關鍵字,Dart 只能實現單一繼承,子類會覆蓋父類同名函式;
2. with 混入Mixin
class People04 with Student { }
class People05 with Teacher { }
class People06 with Teacher { void speak() { print('People06 --> speak'); } }
class People07 with Student, Teacher {}
class People08 with Teacher, Student {}
print('<----------- People04 ----------->');
People04 people04 = People04(); people04.speak();
print('<----------- People05 ----------->');
People05 people05 = People05(); people05.speak();
print('<----------- People06 ----------->');
People06 people06 = People06(); people06.speak();
print('<----------- People07 ----------->');
People07 people07 = People07(); people07.speak();
print('<----------- People08 ----------->');
People08 people08 = People08(); people08.speak();
複製程式碼
- 使用 Mixin 混入時需要使用 with 關鍵字;
- 子類會覆蓋混入類中相同函式;
- 子類可以混入多個類;
- 子類混入多個類時與宣告順序有關係;
小菜簡單理解,子類實現的函式以 with 宣告後面的為準,以 People07 為例,People07 優先實現 Student 中的 speak();之後再實現 Teacher 中的 speak() 函式以覆蓋 Student 類中混入的 speak() 函式;若 People07 子類還有同名函式,會繼續覆蓋 Teacher 類中混入的 speak() 函式;
3. extends 繼承 + with 混入Mixin
class People09 extends Student with Teacher {}
class People10 extends Teacher with Student {}
class People11 extends Student with Teacher, People {}
class People12 extends Student with Teacher, People { void speak() { print('People12 --> speak'); } }
print('<----------- People09 ----------->');
People09 people09 = People09(); people09.speak();
print('<----------- People10 ----------->');
People10 people10 = People10(); people10.speak();
print('<----------- People11 ----------->');
People11 people11 = People11(); people11.speak();
print('<----------- People12 ----------->');
People12 people12 = People12(); people12.speak();
複製程式碼
小菜嘗試 extends 繼承和 with 混入同時應用時,子類最終的結果與上面提及的宣告順序有關;整體的執行順序:extends 繼承優先執行,之後是 with 混入,之後是子類同名函式覆蓋;
4. implements 介面
class People13 implements People {
@override
void speak() {
// TODO: implement speak
print('People13 --> speak');
}
}
複製程式碼
Dart 與 Java 不同,沒有 interface 介面,但 Dart 每個類都有一個隱式的介面,這個介面包含類裡的所有成員變數,以及定義的方法;子類可以實現這個隱式的介面;當父類用作隱式的介面時,子類需要過載 @override 父類的方法;
5. implements 介面 + with 混入Mixin
class People14 with Student implements People {
@override
void speak() {
// TODO: implement speak
super.speak();
print('People14 --> speak');
}
}
class People15 with Student, Teacher implements People {
@override
void speak() {
// TODO: implement speak
super.speak();
print('People15 --> speak');
}
}
複製程式碼
小菜嘗試 with 混入和 implements 介面同時應用時,依舊是需要過載父類的方法;但此時可以通過 super.speak() 使用 with 混入的函式方法;
6. extends 繼承 + with 混入Mixin + implements 介面
mixin Doctor { void speak(); }
class People16 extends People with Student, Teacher implements Doctor {
@override
void speak() {
// TODO: implement speak
super.speak();
print('People16 --> speak');
}
}
複製程式碼
小菜嘗試 extends / with / implements 三者同時應用時,其執行順序是 extends 繼承優先執行,之後是 with 混入,最後是 implements 介面過載;
注意事項:
- 一個類不能同時被用作繼承和介面;
// 錯誤
class People extends People implements People {}
複製程式碼
- 使用 with 混入 Mixin 類時,父類不能含有建構函式;
// 錯誤
class Teacher {
Teacher();
void speak() {
print('Teacher --> speak');
}
}
// 異常提示:Teacher 不能用作 mixin 因為 Teacher 中宣告瞭建構函式
class People with Teacher {}
複製程式碼
- with 混入的類可以用 mixin 修飾,作為抽象的混入方法,此時不能被 extends 繼承,但可以用作隱式介面;
// 錯誤
mixin Teacher { void speak() { print('Teacher --> speak'); } }
// 異常提示:Teacher 不能繼承
class People extends Teacher {}
複製程式碼
- on 可以用於被 mixin 修飾的類,類似於繼承的父類;
class People { void speak() { print('People --> speak'); } }
class Student extends People { void speak() { print('Student --> speak'); } }
mixin Teacher on People { void speak() { print('Teacher --> speak'); } }
複製程式碼
小菜簡單理解 Mixin 可以作為多繼承的一種思想方式,只是要通過非繼承的方式來複用類中的函式程式碼;小菜對於 Mixin 的瞭解還不夠深入,如有錯誤,請多多指導!
來源: 阿策小和尚