dart 语言基础 ->混入(with)关键字
Mixins : 指能够将另一个或多个类的功能添加到您自己的类中,而无需继承这些类。
implements : 将一个类作为接口使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| class A { void a() { print('fun a => by a'); } }
class B implements A { @override void a() { print('fun a => by b'); } }
class C {
void a() { print('fun a => by c'); }
void c() { print('fun c => by c'); }
void s(){ print('fun s => by c'); } }
class E { String e = 'eeee';
void s(){ print('fun s => by e'); } }
class D extends A with C, E { void c() { print('fun c => by d'); } }
void main() { D d = new D(); d.a(); d.s(); d.c(); }
============ output ============ fun a => by c fun s => by e fun c => by d
|
混入是多继承,当被混入的类有多个同名方法时,调用子类的该方法时,会调用 with 声明的最后一个拥有该方法的类中的该方法