博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Define Interfaces and Share Class Members through Mixins in Dart
阅读量:4983 次
发布时间:2019-06-12

本文共 2553 字,大约阅读时间需要 8 分钟。

In this lesson, we will cover Interfaces and Mixins. Interfaces act as a contract containing properties and methods that a class must define once it “implements” this interface. Mixins are Dart’s way of sharing properties and methods from multiple classes, since by design Dart adopts a single-inheritance model.

 

Abstract class:

void main() {  var pixel = Phone('Pixel XL', 'HTC');  pixel.getDeviceInfo();}abstract class Device {  String name;  String manufacturer;  void getDeviceInfo();}class Phone implements Device {  String name;  String manufacturer;    void getDeviceInfo() => print('''  ===  Device name: $name  Manufactured by: $manufacturer  ''');    Phone(this.name, this.manufacturer);}

  

Define a mixin:

mixin FeaturesMixin {  bool blueTooth = true;  bool dualSim = false;  bool nfc = true;}

 

Extends a mixin:

// Extends FeaturesMixinmixin UtilitiesMixin on FeaturesMixin {  bool calculator = true;  bool flashlight = true;  bool thermometer = false;    String _has(bool feat) => feat ? 'Yes': 'No';    void getAllFeatures() => print('''  --FEATURES--    Bluetooth: ${_has(super.blueTooth)}  Dual SIM: ${_has(super.dualSim)}  NFC: ${_has(super.nfc)}  Calculator: ${_has(calculator)}  Flashlight: ${_has(flashlight)}  Thermometer: ${_has(thermometer)}  ===  ''');}

 

use Mixin:

class Phone with FeaturesMixin, UtilitiesMixin implements Device {

 

--

void main() {  var pixel = Phone('Pixel XL', 'HTC');  pixel.getDeviceInfo();  pixel.getAllFeatures(); /*  ===  Device name: Pixel XL  Manufactured by: HTC    --FEATURES--    Bluetooth: Yes  Dual SIM: No  NFC: Yes  Calculator: Yes  Flashlight: Yes  Thermometer: No  ===  */}mixin FeaturesMixin {  bool blueTooth = true;  bool dualSim = false;  bool nfc = true;}// Extends FeaturesMixinmixin UtilitiesMixin on FeaturesMixin {  bool calculator = true;  bool flashlight = true;  bool thermometer = false;    String _has(bool feat) => feat ? 'Yes': 'No';    void getAllFeatures() => print('''  --FEATURES--    Bluetooth: ${_has(super.blueTooth)}  Dual SIM: ${_has(super.dualSim)}  NFC: ${_has(super.nfc)}  Calculator: ${_has(calculator)}  Flashlight: ${_has(flashlight)}  Thermometer: ${_has(thermometer)}  ===  ''');}abstract class Device {  String name;  String manufacturer;  void getDeviceInfo();}class Phone with FeaturesMixin, UtilitiesMixin implements Device {  String name;  String manufacturer;    void getDeviceInfo() => print('''  ===  Device name: $name  Manufactured by: $manufacturer  ''');    Phone(this.name, this.manufacturer);}

 

转载于:https://www.cnblogs.com/Answer1215/p/11409024.html

你可能感兴趣的文章
调查问卷心得体会
查看>>
Linux文件3个时间点(access time,modify time,change time)
查看>>
深谈德国车和日本车的区别--觉得分析的还算冷静客观
查看>>
C#命名空间
查看>>
poj1655Multiplication Puzzle
查看>>
WinDebug 常用命令表【摘】
查看>>
LVS _keepalived 配置
查看>>
Django之ORM基础
查看>>
JS监听浏览器关闭事件
查看>>
[Log]ASP.NET之HttpModule 事件执行顺序
查看>>
明天回老家看我儿子了!
查看>>
hdu2089(数位dp模版)
查看>>
JS 获取浏览器和屏幕宽高信息
查看>>
TCP/UDP 协议,和 HTTP、FTP、SMTP,区别及应用场景
查看>>
我的大学生活
查看>>
php SPL四种常用的数据结构
查看>>
计算tableview的高度
查看>>
使用外语会影响我们的道德判断
查看>>
菜鸟学Java第一天
查看>>
【freemaker】之自定义指令通用select模版
查看>>