Dart 是不支持多繼承的檬洞,但是它支持 mixin聚霜,簡單來講 mixin 可以 “組合” 多個類屈呕,如果多個mixin 中有同名方法微宝,with 時,會默認使用最后面的 mixin 的虎眨,mixin 方法中可以通過 super 關(guān)鍵字調(diào)用之前 mixin 或類中的方法蟋软。
abstract class Configure {
config() {}
}
mixin One on Configure {
@override
config() {
print("up one");
super.config();
print("one");
}
}
mixin Two on Configure {
@override
config() {
print("up two");
super.config();
print("two");
}
}
mixin Three on Configure {
@override
config() {
print('up three');
super.config();
print('three');
}
}
class Zero extends Configure with One, Two, Three {
@override
config() {
super.config();
}
}
void main(List<String> args) {
Zero zero = Zero();
zero.config();
}
// $dart test1.dart
// up three
// up two
// up one
// one
// two
// three