Mixin(混入)。
有下面一個場景:
狗,鳥,人都是動物。
鳥會飛。
狗會跑,會游泳。
人會跑,會游泳,會飛?
抽象這種場景,對于單繼承語言來說,我們有接口(interface),來規(guī)范,抽象事物共同的行為,約束,規(guī)范,Dart 雖然沒有 interface 關(guān)鍵字,但也不影響。其實(shí)現(xiàn)如下:
abstract class Animal {
}
abstract class Run {
void run() => print("會跑");
}
abstract class Fly {
void fly() => print("會飛");
}
abstract class Swim {
void swim() => print("會游泳");
}
class Bird extends Animal implements Fly {
@override
void fly() {
// TODO: implement fly
//super.run();//編譯報錯,它會認(rèn)為抽象類 Fly中的fly()方法是抽象的(真實(shí)的并不是抽象的),
print("會飛");
}
}
class Dog extends Animal implements Run, Swim {
@override
void run() {
// TODO: implement run
print("會跑");
}
@override
void swim() {
// TODO: implement swim
print("會游泳");
}
}
class Person extends Animal implements Run, Swim, Fly {
@override
void fly() {
// TODO: implement fly
print("會飛");
}
@override
void run() {
// TODO: implement run
print("會跑");
}
@override
void swim() {
// TODO: implement swim
print("會游泳");
}
}
從上面實(shí)現(xiàn)代碼來了看,還是存在問題的,Bird,Dog,PerSon 實(shí)現(xiàn)接口的時候都要實(shí)現(xiàn)接口里面的方法,但其實(shí)可以看到 run(),swim()方法都是一樣的。秉著消滅重復(fù)代碼的原則來說,接口這種實(shí)現(xiàn)還是有點(diǎn)點(diǎn)問題的。
這里提一下 Java8 接口里面的 default 關(guān)鍵字。
Java8 之前,接口是不能有實(shí)體方法的,但是現(xiàn)在可以了,它是這么操作的。
public interface Face {
default void HelloWorld(){
System.out.println("Default Hello World ");
}
}
class FaceImpl implements Face {
}
public class Test {
public static void main(String[] args) {
FaceImpl face= new FaceImpl();
face.HelloWorld();
}
}
FaceImpl 可以不用實(shí)現(xiàn) HelloWorld(),而可以直接調(diào)用它,那 default 有啥用?Java8 之前的接口,每當(dāng)我們在擴(kuò)展接口功能的時候,對應(yīng)接口的實(shí)現(xiàn)類也必須重寫實(shí)現(xiàn)擴(kuò)展接口的方法。這時候可能改動就很大。如果這種接口功能的擴(kuò)展改動就在 SDK 里面,那改動會相當(dāng)?shù)拇?。?default 打破了 Java 之前版本對接口的語法限制(接口里面只能有抽象方法 ),從而使得接口在進(jìn)行擴(kuò)展的時候,不會破壞與接口相關(guān)的實(shí)現(xiàn)類代碼。
說這些是為了更好的認(rèn)識 Mixin。
在 dart 中,有能更好的實(shí)現(xiàn)上面場景的方式:
abstract class Animal {}
mixin Run {
void run() => print("會跑");
}
mixin Fly {
void fly() => print("會飛");
}
mixin Swim {
void swim() => print("會游泳");
}
class Bird extends Animal with Fly {
@override
void fly() {
super.fly(); //合情合理合法
}
}
class Dog extends Animal with Run, Swim {
@override
void run() {
super.run();
}
@override
void swim() {
// TODO: implement swim
super.swim();
}
}
class Person extends Animal with Run, Swim, Fly {
@override
void fly() {
super.fly(); //合情合理合法
}
@override
void run() {
super.run();
}
@override
void swim() {
// TODO: implement swim
super.swim();
}
}
void main() {
Person person = new Person();
person.fly();
person.run();
person.swim();
}
可見這種實(shí)現(xiàn)方式主要的關(guān)鍵字就是 mixin,with。
mixin 能減少代碼冗余,能在多個類層次結(jié)構(gòu)中復(fù)用代碼,相比約束性很強(qiáng)的接口來說顯得更加自由。
minxin 是一種特殊的多重繼承,適用于以下場景:
- 希望為類提供許多可選功能.
- 希望在許多不同的類中使用一個特定的功能.
Mixin 的特點(diǎn)
線性化
如果在兩個 Mixin 模塊都有一個同名方法,這兩個模塊同時混入一個類,那最終調(diào)用的會是哪個模塊的方法呢?寫個代碼跑一下。。。
//Object
class O {
String getMessage() => "O";
}
mixin A {
String getMessage() => 'A';
}
mixin B {
String getMessage() => "B";
}
class AB extends O with A, B {}
class BA extends O with B, A {}
void main() {
String result1 = "";
String result2 = "";
AB ab = AB();
result1= ab.getMessage();
print("ab is ${ab is A}");
print("ab is ${ab is B}");
print("ab is ${ab is O}");
BA ba = BA();
result2 += ba.getMessage();
print("ba is ${ba is B}");
print("ba is ${ba is A}");
print("ba is ${ba is O}");
print(result1);
print(result2);
}
輸出
ab is true
ab is true
ab is true
ba is true
ba is true
ba is true
B
A
由此可知:
- with 修飾的會覆蓋 extends 中修飾的同名方法.
- with 列表中后一個模塊功能會覆蓋之前的
Mixin 的 限定
對類的限定
我們限定行走這種功能行為只能在動物身上:
class Animal {}
mixin Walk on Animal {
void walk() {
print("會走路了");
}
}
class ZhiWu with Walk {} //這是錯誤的寫法,編譯通過不了
class Person extends Animal with Walk {}
可以看出 on 關(guān)鍵字限定了 Walk 這種功能只能在 Animal 的子類混入。
對功能模塊的限定
有一種場景,類似人要先學(xué)會走路,然后慢慢才會跳舞,也可能一輩子也不會。。。
class Animal{
}
mixin Walk {
void walk() {
print("會走路了");
}
}
mixin Dance on Animal, Walk {
void dance() {
print("居然會跳舞了");
}
}
//class Person with Dance {} 錯誤的寫法,編譯不通過
//class Person with Walk, Dance {} 錯誤的寫法,編譯不通過
class Person extends Animal with Walk, Dance {}
void main() {
Person person = new Person();
person.walk();
person.dance();
}
可以看出要想會跳舞,必需是動物,必須學(xué)會走路。
小結(jié)
mixin: 定義了功能模塊。
on: 限定了功能模塊的使用類型。
with: 負(fù)責(zé)功能模塊的組合。
最后
貼一張自己學(xué)習(xí)Flutter的公眾號,感興趣的小伙伴可以一起學(xué)習(xí)哦。。。
