[Dart] Mixin
Posted answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Dart] Mixin相关的知识,希望对你有一定的参考价值。
Mixins are a way of reusing a class’s code in multiple class hierarchies.
void main() { Animal animal = Animal(); Bird().fly(); Fish().swim(); Duck().move(); Duck().swim(); Duck().fly(); } mixin CanSwim { void swim() { print(‘change poisiton by swimming‘); } } mixin CanFly { void fly() { print(‘change poisiton by flying‘); } } class Animal { void move () { print(‘change position‘); } } class Fish extends Animal with CanSwim{ @override void move () { super.move(); } } class Bird extends Animal with CanFly{ @override void move () { super.move(); } } class Duck extends Animal with CanFly, CanSwim{ @override void move () { super.move(); } }
以上是关于[Dart] Mixin的主要内容,如果未能解决你的问题,请参考以下文章
flutter解决 dart:html 只支持 flutter_web 其他平台编译报错 Avoid using web-only libraries outside Flutter web(代码片段