Android学Dart学习笔记第十九节 类-混入Mixins

Mixins 就像是可插拔的功能模块,可以给多个不同的类批量添加相同的功能实现,而不需要这些类有共同的父类。

要使用mixin功能,请使用with关键字,后跟一个或多个mixin名称。下面的例子展示了两个使用mixins的类(或者它们是mixins的子类):

dart 复制代码
class Musician extends Performer with Musical {
  // ···
}

class Maestro extends Person with Musical, Aggressive, Demented {
  Maestro(String maestroName) {
    name = maestroName;
    canConduct = true;
  }
}

要定义一个mixin,使用mixin声明。在需要同时定义mixin和类的罕见情况下,可以使用mixin类声明。

mixin和mixin类不能有extends子句,并且不能声明任何生成构造函数。

声明案例如下:

dart 复制代码
mixin Musical {
  bool canPlayPiano = false;
  bool canCompose = false;
  bool canConduct = false;

  void entertainMe() {
    if (canPlayPiano) {
      print('Playing piano');
    } else if (canConduct) {
      print('Waving hands');
    } else {
      print('Humming to self');
    }
  }
}

Specify members a mixin can call on itself

指定mixin可以调用自身的成员

有时,mixin依赖于能够调用方法或访问字段,但不能自己定义这些成员(因为mixin不能使用构造函数的参数来实例化自己的字段)。

下面几节将介绍确保mixin的任何子类定义mixin行为所依赖的任何成员的不同策略。

Define abstract members in the mixin

在mixin中声明抽象方法会强制任何使用mixin的类型定义其行为所依赖的抽象方法。

dart 复制代码
mixin Musician {
  void playInstrument(String instrumentName); // Abstract method.

  void playPiano() {
    playInstrument('Piano');
  }
  void playFlute() {
    playInstrument('Flute');
  }
}

class Virtuoso with Musician {

  @override
  void playInstrument(String instrumentName) { // Subclass must define.
    print('Plays the $instrumentName beautifully');
  }
}
Mixin 可以定义抽象成员,访问子类的状态

下面的例子可以应用于任何带有[name]属性的类型,并提供实现了[hashCode]和运算符' == '。

dart 复制代码
mixin NameIdentity {
  String get name;

  @override
  int get hashCode => name.hashCode;

  @override
  bool operator ==(other) => other is NameIdentity && name == other.name;
}

class Person with NameIdentity {
  final String name;

  Person(this.name);
}

Implement an interface

与声明mixin抽象类似,在不实际实现接口的同时在mixin上放置一个实现子句也将确保为mixin定义任何成员依赖项。

dart 复制代码
abstract interface class Tuner {
  void tuneInstrument();
}

mixin Guitarist implements Tuner {
  void playSong() {
    tuneInstrument();

    print('Strums guitar majestically.');
  }
}

class PunkRocker with Guitarist {

  @override
  void tuneInstrument() {
    print("Don't bother, being out of tune is punk rock.");
  }
}

Use the on clause to declare a superclass

on 子句的作用是定义 super 调用所解析的类型。因此,只有当你在 mixin 中需要进行 super 调用时才应该使用它。

on 子句强制任何使用该 mixin 的类也必须是 on 子句中类型的子类。这确保了如果 mixin 依赖于超类中的成员,那么在使用 mixin 的地方这些成员是可用的。

如果M是个mixin同时on了A,如果你想在B类中混入M,那么B必须是A的子类。

dart 复制代码
class Musician {
  musicianMethod() {
    print('Playing music!');
  }
}

mixin MusicalPerformer on Musician {
  performerMethod() {
    print('Performing music!');
    super.musicianMethod();
  }
}

class SingerDancer extends Musician with MusicalPerformer { }

main() {
  SingerDancer().performerMethod();
}

class, mixin, or mixin class?

mixin class声明需要至少3.0的语言版本。

Mixin 声明定义一个 mixin。class声明定义一个类。Mixin class声明定义一个既可以作为普通类使用,又可以作为 mixin 使用的类,具有相同的名称和相同的类型。

dart 复制代码
mixin class Musician {
  // ...
}

class Novice with Musician { // Use Musician as a mixin
  // ...
}

class Novice extends Musician { // Use Musician as a class
  // ...
}

适用于类或 Mixin 的任何限制也适用于 Mixin 类:

Mixin 不能有 extends 或 with 子句,所以 Mixin 类也不能有。

类不能有 on 子句,所以 Mixin 类也不能有。

相关推荐
xiaobai17811 小时前
测试工程师入门AI技术 - 前序:跨越焦虑,从优势出发开启学习之旅
人工智能·学习
北岛寒沫11 小时前
北京大学国家发展研究院 经济学原理课程笔记(第二十一课 金融学基础)
经验分享·笔记·学习
alexhilton11 小时前
Jetpack Compose内部的不同节点类型
android·kotlin·android jetpack
扑火的小飞蛾11 小时前
网络安全小白学习路线图 (基于提供文档库)
学习·安全·web安全
优雅的潮叭11 小时前
c++ 学习笔记之 malloc
c++·笔记·学习
Frank_HarmonyOS12 小时前
Android中四大组件之一的Activity的启动模式
android
薛不痒13 小时前
深度学习之优化模型(数据预处理,数据增强,调整学习率)
深度学习·学习
似霰13 小时前
HIDL Hal 开发笔记7----简单 HIDL HAL 实现
android·framework·hal
昵称已被吞噬~‘(*@﹏@*)’~13 小时前
【RL+空战】学习记录03:基于JSBSim构造简易空空导弹模型,并结合python接口调用测试
开发语言·人工智能·python·学习·深度强化学习·jsbsim·空战
我想我不够好。13 小时前
学到的知识点 1.8
学习