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 FeaturesMixin
mixin 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 FeaturesMixin
mixin 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);
}

Define Interfaces and Share Class Members through Mixins in Dart的更多相关文章

  1. sencha touch extend 单继承 和 mixins 实现多继承

    继承可以达到代码的复用,利于维护和扩展. sencha touch 中可以通过 extend 实现单继承,通过 mixins 实现多继承. mixins 也很像实现接口,不过这些接口的方法已经实现了, ...

  2. Generic Interfaces (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/kwtft8ak(v=vs.140).aspx It is often useful to define interf ...

  3. [Sencha ExtJS & Touch] 在Sencha(Extjs/Touch)应用程序中使用plugins(插件)和mixins(混入)

    原文链接:http://blog.csdn.net/lovelyelfpop/article/details/50853591 英文原文:Using Plugins and Mixins in You ...

  4. http://wiki.apache.org/tomcat/HowTo

    http://wiki.apache.org/tomcat/HowTo Contents Meta How do I add a question to this page? How do I con ...

  5. Dart 基础重点截取 Dart 2 20180417

    官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...

  6. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

  7. Thinking in Java——笔记(15)

    Generics The term "generic" means "pertaining or appropriate to large groups of class ...

  8. 【转】ASP.NET MVC 的最佳实践

    [This post is based on a document authored by Ben Grover (a senior developer at Microsoft). It is ou ...

  9. [转]UIWebView的Javascript运行时对象

    An alternative, that may get you rejected from the app store, is to use WebScriptObject. These APIs ...

随机推荐

  1. todo---java中的json探讨

    1.json的命名格式 2.json赋值原则 3.json常用的工具 4.json的处理的第三方软件比较 5.json的起源 6.关于json串的对于null ,"" 的不同的第三 ...

  2. Python22之lambda表达式

    一.Lambda表达式的概念和意义 lambda表达式有称为隐函数,它的定义过程由形参和要返回的表达式组成,它相对于一般函数而言具有以下优势: 1.免去了函数定义的过程,代码变得更加精简 2.省却函数 ...

  3. leetcode 2019.10.29 首次破百

    刷题首次破百,记录一下自己成长的历程. 仍在路上,会慢慢变强的~

  4. c++实现的顺序栈

    栈是一种运算受限的线性表,是一种先进后出的数据结构,限定只能在一端进行插入和删除操作,允许操作的一端称为栈顶,不允许操作的称为栈底 因此需要的成员变量如下 int *_stack; //指向申请的空间 ...

  5. pb笔记之数据窗口设置操作

    1 使DataWindow列只能追加不能修改如何使DataWindow中的数据只能追加新记录而不能修改,利用 Column 的 Protect 属性可以很方便的做到这一点,方法如下:将每一列的 Pro ...

  6. 使用Jenkins编译打包SpringCloud微服务中的个别目录

    意义说明: 使用Jenkins从Gogs拉取SpringCloud微服务,拉取的是整个仓库的内容,分好多个模块文件夹,但是使用maven编译打包的话只编译打包指定的模块文件夹 Gogs Webhook ...

  7. k8s 开源web操作平台

    https://kuboard.cn/install/install-dashboard.html kuborad

  8. VBA Exit For语句

    当想要根据特定标准退出For循环时,就可以使用Exit For语句.当执行Exit For时,控件会立即跳转到For循环之后的下一个语句. 语法 以下是在VBA中Exit For语句的语法. Exit ...

  9. 【转载】C#将字符串中字母全部转换为大写或者小写

    在C#的编程开发过程中,有时候判断字符串是否相等时,并不关注字母的大小写,此时在C#中可以使用ToUpper方法将字符串中所有的字母转换为大写,使用ToLower方法可以将字符串中所有字母转换为小写. ...

  10. Java 之 ObjectOutputStream 类

    ObjectOutputStream 类 1.概述 java.io.ObjectOutputStream extends OutputStream ObjectOutputStream:对象的序列化流 ...