Flutter-3 Dart 中的Mixin (混入)

本文详细介绍了Dart中的Mixin混入机制,包括其定义、优势、使用方式(with关键字和mixin声明),以及混入的线性化分析。通过实例解析了混入如何解决多继承和代码复用问题,同时探讨了混入的类型和调用顺序规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Mixin[混入]是什么

  1. dart官网解释: Mixins是一种可以在多个类层次结构中复用类代码的方式;
  2. 简单来说:它是dart中 除了继承 和 接口实现的另外一种,类的代码复用的方式。

二、Mixin[混入]的优势

  1. 继承【extends】:代码复用性强,子类可以根据需要覆写父类的成员变量和 方法,但是只能单继承
  2. 接口实现【implements】:在dart中 一个类可以被继承也可以被实现,接口实现只能获取到父类的空壳子,子类需要覆写 父类的变量和 方法实现, 接口实现虽然可以多实现,但是很多重复代码让子类去覆写显得冗余;Java8 和 Kotlin 选择使用接口的 default 实现来解决这个问题,Dart使用mixin【混入】
  3. 混入【mixin+ with】:由于类的单继承,和 多实现中子类代码的冗余,所以Dart 引入Mixin【混入】机制; mixin【混入】的关键字with后能接多个类,解决了不支持多继承的问题,同时,子类也可以选择性的重写父类的方法;可以理解为 带有方法实现的接口,它可以解决缺乏多继承的支持;

三、Mixin[混入]使用方式

方式一:普通类使用 with 关键字

// 普通类
class Parent {
  num x = 0, y = 0;
  void printInfo() => print('($x,$y)');
}
// with + 普通类
class TestA with Parent {

}
// main打印
void main() {
  var testA = TestA();
  // 可以获取成员变量和方法
  testA.x = 100;
  testA.y = 20;
  testA.printInfo();
}
// 输出结果
(100,20)

方式二:声明类时 mixin代替class关键字 ,使用时 with关键字

使用mixin声明类的特点:

  1. 用mixin修饰的类不能声明构造函数 所以 也不能实例化
  2. 用mixin修饰的类不能被继承 只能使用with关键字混入
// mixin 声明只能混入的类
mixin MixinParentOne{
  void test() => print('test mixin parentOne');
}
// with
class TestB with MixinParentOne {}
// main打印
void main() {
  var testB = TestB();
  testB.test();
}
// 输出结果
test mixin parentOne

四、Mixin[混入]线性化分析

首先探索个问题:with关键字后有多个类,并且多个类中有相同的方法,那么当调用该方法时,会调用哪个类中的方法?

场景一:当前使用类重写了该同名方法

// 普通类S
class S {
  fun(String from)=>print('currentClassNamee:S  from:$from');
}
// mixin声明类MA
mixin MA on S {
  @override 
  fun(String from) {
    print('currentClassName:MA  from:$from');
    super.fun("MA");
  }
}
// 继承s  混入MA  重写同名方法
class A extends S with MA {
   @override
   fun(String from) {
    print('currentClassName:A  from:$from');
  }
}
// main 函数执行
void main() {
    A a = A();
    a.fun("main");
}

// 打印结果
currentClassName:A  from:main

结论:如果当前使用类重写了该方法,就会调用当前类中的方法。

场景二:当前使用类没有重写同名方法

class S {
  fun(String from)=>print('currentClassName:S  from:$from');
}
mixin MA on S {
  fun(String from) {
    print('currentClassName:MA  from:$from');
  }
}
mixin MB on S {
  fun(String from) {
    print('currentClassName:MB  from:$from');
  }
}
mixin MC on S {
  fun(String from) {
    print('currentClassName:MC  from:$from');
  }
}

class A extends S with MC,MA,MB {}
class B extends S with MB,MA {}

void main() {
  A a = A();
  a.fun("main");

  B b = B();
  b.fun('main');
}
// 执行结果
currentClassName:MB  from:main
currentClassName:MA  from:main

Mixin特性:
如果class继承或者实现了多个类,同一个方法调用的时候,实际使用的是哪个方法优先级如下with>extend>implements,如果with后面跟了多个类,后面的优先级大于前面的,也就是说覆盖前边的同名方法

Mixin的线性分析

添加super方法调用如下

class S {
  fun(String from) => print('currentClassName:S  from:$from');
}

mixin MA on S {
  fun(String from) {
    super.fun("MA");
    print('currentClassName:MA  from:$from');
  }
}
mixin MB on S {
  fun(String from) {
    super.fun("MB");
    print('currentClassName:MB  from:$from');
  }
}
class A extends S with MA, MB {}

void main() {
  A a = A();
  a.fun("main");
  B b = B();
  b.fun('main');
}
// 打印结果
currentClassName:S  from:MA
currentClassName:MA  from:MB
currentClassName:MB  from:main
currentClassName:S  from:MB
currentClassName:MB  from:MA
currentClassName:MA  from:main

以A为例
Mixin[混入] 是具有线性化特性,每次通过混合和复用一系列操作,生成多个中间的mixin类,这个过程是线性化的,最终会继承 混入后的最终类;实际上还是单继承

class A extends S with MA, MB {}

由于线性化 语义理解可以分以下几步:

//S with MA 会产生S MA类混合体,MA类中方法会覆盖S类中的同名方法,那么SMA中间类保留是MA类中的方法
class SMA = S with MA;
//SMA with MB 会产生SMAMB类混合体,MB类中方法会覆盖SMA混合类中的方法,那么SMAMB中间类保留MB中方法
class SMAMB = SMA with MB;
//最终A类相当于继承的是SMAMB类混合体,保留的方法是MB类中的同名方法
class A extends SMAMB {}

大概图解如下
在这里插入图片描述

分析 类A的方法调用顺序

  1. a.fun(“main”); 调用的是 SMAMB混合类 中的fun方法 ,由上分析 保留的是MB类中的同名方法
  2. MB中的方法 SMAMB 先调用的super.fun(“MB”); 再打印自身;
  3. 理解super.fun(“MB”); 其实是上述生成中间类的上一层,即调用的是SMA中的fun方法,混合体SMA保留的是MA里的fun方法 ,MA里的fun方法也是先调用super.fun(“MA”); 即先调用S里的fun方法
  4. 所以先输出 currentClassName:S from:MA
  5. 然后输出 MA里super()后的方法 currentClassName:MA from:MB
  6. 然后输出MB里的fun方法 currentClassName:MB from:main

五、Mixin[混入]的类型

首先看类A 类型

class S {
  fun(String from) => print('currentClassName:S  from:$from');
}

mixin MA on S {
  fun(String from) {
    super.fun("MA");
    print('currentClassName:MA  from:$from');
  }
}
mixin MB on S {
  fun(String from) {
    super.fun("MB");
    print('currentClassName:MB  from:$from');
  }
}

class A extends S with MA, MB {}

void main() {
  A a = A();
  print(a is S);
  print(a is MA);
  print(a is MB);
}
//结果
true
true
true

解释:因为mixin with后就会生成新的中间类,比如中间类SMA、SMAMB. 同时也会生成 一个新的接口SMA、SMAMB(因为所有Dart类都定义了对应的接口, Dart类是可以直接当做接口实现的)。新的中间类SMAMB继承基类S以及S与MA中间类副本,同时也可以认为它也实现了每一个接口【类也是接口】。SMAMB混合类最后会实现了S、MA、MB三个接口,最后类A继承SMAMB类实际上也相当于间接实现了S、MA、MB三个接口,所以类A肯定是S、MA、MB的子类型,故所有输出都是true。

中间类过程如下:

class S {
  fun(String from) => print('currentClassName:S  from:$from');
}

mixin MA on S {
  fun(String from) {
    super.fun("MA");
    print('currentClassName:MA  from:$from');
  }
}
mixin MB on S {
  fun(String from) {
    super.fun("MB");
    print('currentClassName:MB  from:$from');
  }
}

class SMA = S with MA;

class SMAMB = SMA with MB;

class Test extends SMAMB {}

void main() {
  A a = A();

  Test test = Test();
  print(test is S);
  print(test is MA);
  print(test is MB);
  print(test is SMA);
  print(test is SMAMB);
}
// 结果
true
true
true
true
true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值