小菜在简单学习源码过程中经常遇到 mixin 类型的 Class 类,而小菜之前是做 Android 开发的,Java / Kotlin 中并没有 mixin 的概念,小菜今天简单了解一下;
Mixin
基本介绍
Mixin 是一种在多个类层次结构中重用类代码的方法;小菜查阅了很多资料,比较官方的介绍是:
Mixin 是面向对象程序设计语言中的类,提供了方法的实现,其他类可以访问 Mixin 类的方法而不必成为其子类;Mixin 为使用它的 Class 类提供额外的功能,但自身却不单独使用(不能单独生成实例对象,属于抽象类),Mixin 类通常作为功能模块使用,在需要该功能时“混入”,而且不会使类的关系变得复杂;
Mixin 有利于代码复用性同时又避免了多继承的复杂性,使用 Mixin 享有单一继承的单纯性和多重继承的共有性,interface 接口与 Mixin 相同的地方是都可以多继承,不同的地方在于 Mixin 是可以实现的;
对应关系
继承 | 混入 | 接口 | |
---|---|---|---|
关键字 | extends | with | implements |
对应数量 | 1:1 | 1:n | 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 的了解还不够深入,如有错误,请多多指导!
来源: 阿策小和尚