mixins
作为Flutter的重要特性,值得好好研究一下
mixins
的中文意思是混入,就是在类中混入其他功能。
Dart中的定义是:
Mixins are a way of reusing a class’s code in multiple class hierarchies. 复制代码
Mixins是一种在多个类层次结构中重用类代码的方法。
可以看出Mixins最重要的功能是重用代码,我们先看下JAVA,重用代码的方式有哪些:
继承
子类可以复用父类的方法和属性,但是JAVA里的继承只能单继承。
组合
将要重用的代码,封装成类A,让其他类持有A的实例,看上去貌似解决了重用代码的问题,但是一方面,每个类持有的A的实例是不同的,有多少个类,就总共有多少个A的实例,而且另一方面,即使A使用单例,使用起来也很不方便。
接口 定义一个接口interface,类实现interface,这样虽然接口是同一个,但是实现却是分散的,能重用的代码是有限的。
所以在JAVA里想要重用代码,限制是很多的。
这就有了 mixins
的概念, mixins
最早的根源来自于Lisp,因为Dart也受到smalltakk的影响,所以Dart引入了 mixins
的概念,
在维基百科中有对 mixins
最准确的定义:
在面向对象的语言中,mixins类是一个可以把自己的方法提供给其他类使用,但却不需要成为其他类的父类。 复制代码
mixins
要重用的代码,不是方法或者是接口,而是类!
这里举个例子,有一个类A,A中有一个方法a(),还有一个方法B,也想使用a()方法,那么这时候就需要用到mixins,类A就是mixins类(混入类),类B就是要被mixins的类,对应的Dart代码如下:
class A {
String content = 'A Class';
void a(){
print("a");
}
}
class B with A{
}
B b = new B();
print(b.content);
b.a();
复制代码
输出是:
A Class a 复制代码
将类A mixins 到 B,B可以使用A的属性和方法,B就具备了A的功能,但是需要强调的是:
mixins的对象是类
mixins绝不是继承,也不是接口,而是一种全新的特性
mixins要用到的关键字 with
怎么来理解 with
呢?很简单:
继承 -> extends
mixins -> with
继承和mixins是一样的,是语言的特性,with和extends是关键字。
因为mixins使用的条件,随着Dart版本一直在变,这里讲的是Dart2.1中使用mixins的条件:
on
看下面代码:
class A {
void a(){
print("a");
}
}
class A1 {
void a1(){
print("a1");
}
}
class B with A,A1{
}
B b = new B();
b.a();
b.a1();
复制代码
输出是:
a a1 复制代码
但是,如果A和A1的方法相同,而且调换A和A1的顺序,在被mixins的类中实现同一个方法呢,看下面的代码:
class A {
void a(){
print("a");
}
}
class A1 {
void a(){
print("a1");
}
}
class B with A,A1{
}
class B1 with A1,A{
}
class B2 with A,A1{
void a(){
print("b2");
}
}
class C {
void a(){
print("a1");
}
}
class B3 extends C with A,A1{
}
class B4 extends C with A1,A{
}
class B5 extends C with A,A1{
void a(){
print("b5");
}
}
B b = new B();
B1 b1 = new B1();
B2 b2 = new B2();
B3 b3 = new B3();
B4 b4 = new B4();
B5 b5 = new B5();
b.a();
b1.a();
b2.a();
b3.a();
b4.a();
b5.a();
复制代码
会是什么样的结果呢?
Mixins in Dart work by creating a new class that layers the implementation of the mixin on top of a superclass to create a new class — it is not “on the side” but “on top” of the superclass, so there is no ambiguity in how to resolve lookups. 复制代码
以
class B3 extends C with A,A1{
}
复制代码
为例,可以分解为:
class CA = C with A;
class CAA1 = CA with A1;
class B3 extends CAA1{
}
复制代码
mixins不是多继承
Mixins is not a way to get multiple inheritance in the classical sense. Mixins is a way to abstract and reuse a family of operations and state. It is similar to the reuse you get from extending a class, but it is compatible with single-inheritance because it is linear. 复制代码
所以输出结果是:
a1 a b2 a1 a b5 复制代码