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 类也不能有。

相关推荐
双眼鈹8 分钟前
周报8.31
笔记·学习
打团从来人不齐35 分钟前
Linux服务器搭建笔记-008:台式机共享WIFI
linux·服务器·笔记
杉氧40 分钟前
React 灵魂:深入剖析 Hooks 与副作用(Side Effects)陷阱
android·前端·react native
疯狂打码的少年1 小时前
【数据库技术】SQL概述与数据定义(DDL:CREATE/DROP/ALTER)
数据库·笔记·sql·oracle
硬件yun1 小时前
学功能安全
学习·安全·汽车
火眼金睛炼单词1 小时前
图像记单词:视觉化学习能否破解英语记忆难题
学习
一条破秋裤1 小时前
STM32 学习笔记:GPIO 输出原理与硬件设计
笔记·stm32·学习
andrsted1 小时前
作业发布、课堂检测和错题讲评,用一个小程序完成
学习·微信小程序·小程序·学习方法
摇滚侠1 小时前
《SpringBoot 3:入门与应用实战》第 9 章 使用 WebMvc 开发进阶 阅读笔记 24
spring boot·笔记·后端
又见情义1 小时前
RK3568 + RTL8211F 网络唤醒(WOL)功能适配全记录
android·网络·驱动开发