Dart 基础之Mixin(混入)

4,434 阅读4分钟

为什么有 Mixin

Mixin(混入)。

有下面一个场景:

狗,鸟,人都是动物。
鸟会飞。
狗会跑,会游泳。
人会跑,会游泳,会飞?

抽象这种场景,对于单继承语言来说,我们有接口(interface),来规范,抽象事物共同的行为,约束,规范,Dart 虽然没有 interface 关键字,但也不影响。其实现如下:

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();//编译报错,它会认为抽象类 Fly中的fly()方法是抽象的(真实的并不是抽象的),
    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("会游泳");
  }
}

从上面实现代码来了看,还是存在问题的,Bird,Dog,PerSon 实现接口的时候都要实现接口里面的方法,但其实可以看到 run(),swim()方法都是一样的。秉着消灭重复代码的原则来说,接口这种实现还是有点点问题的。

这里提一下 Java8 接口里面的 default 关键字。

Java8 之前,接口是不能有实体方法的,但是现在可以了,它是这么操作的。

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 可以不用实现 HelloWorld(),而可以直接调用它,那 default 有啥用?Java8 之前的接口,每当我们在扩展接口功能的时候,对应接口的实现类也必须重写实现扩展接口的方法。这时候可能改动就很大。如果这种接口功能的扩展改动就在 SDK 里面,那改动会相当的大。而 default 打破了 Java 之前版本对接口的语法限制(接口里面只能有抽象方法  ),从而使得接口在进行扩展的时候,不会破坏与接口相关的实现类代码。

说这些是为了更好的认识 Mixin。

在 dart 中,有能更好的实现上面场景的方式:

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();
}

可见这种实现方式主要的关键字就是 mixin,with。

mixin 能减少代码冗余,能在多个类层次结构中复用代码,相比约束性很强的接口来说显得更加自由。

minxin 是一种特殊的多重继承,适用于以下场景:

  • 希望为类提供许多可选功能.
  • 希望在许多不同的类中使用一个特定的功能.

Mixin 的特点

线性化

如果在两个 Mixin 模块都有一个同名方法,这两个模块同时混入一个类,那最终调用的会是哪个模块的方法呢?写个代码跑一下。。。



//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 关键字限定了 Walk 这种功能只能在 Animal 的子类混入。

对功能模块的限定

有一种场景,类似人要先学会走路,然后慢慢才会跳舞,也可能一辈子也不会。。。


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();
}


可以看出要想会跳舞,必需是动物,必须学会走路。

小结

mixin: 定义了功能模块。
on: 限定了功能模块的使用类型。
with: 负责功能模块的组合。