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. Word 删除多余空行

    打开查找替换框: 「^p^p」(意思:换行,换行)替换为「^p」 「^p^w^p」(意思:换行,空白区域,换行)替换为「^p」 未完 ...... 点击访问原文(进入后根据右侧标签,快速定位到本文)

  2. 开始使用 Ubuntu(字体渲染去模糊+软件安装+优化配置+常见错误)(29)

    1. 中文字体渲染美化 + 去模糊 步骤: 1. 解压安装 lulinux_fontsConf_181226.tar.gz,按里面的安装说明操作: 2. 开启字体渲染: 打开 unity-tweak- ...

  3. Visual Studio快速入门(大纲)

    安装与配置 下载 配置Visual Studio环境支持C++桌面编程 Hello World 支持C++98 ( Hello World) 支持C++17( Hello World) 配置Visua ...

  4. 专业仿百度百科,维基wiki百科网站开发建设

    专业仿百度百科,维基wiki百科网站开发建设,有需要的朋友可以欢迎私聊我 提供一站式服务:联系QQ:8582-36016(私聊),微信:lianweikj 电话:186-7597-7935 支持终端: ...

  5. Android—网络请求

    import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; ...

  6. Docker 学习笔记(二):Dockerfile 定制镜像

    镜像的定制实际上就是定制每一层所添加的配置.文件. 如果我们可以把每一层修改.安装.构建.操作的命令都写入一个脚本,用这个脚本来构建.定制镜像,那么之前提及的无法重复的问题.镜像构建透明性的问题.体积 ...

  7. [PKUSC2018]主斗地(搜索+贪心)

    首先如果对子和三张牌出现在解中,那么全拆成单张显然没有问题,顺子同理.于是真正有用的牌型就只有单牌.三带一.三带二.四带二了. 暴搜jry手中的牌,然后先搜出双方的大牌型(即三张.四张牌的个数),再枚 ...

  8. 关于MQ的几件小事(五)如何保证消息按顺序执行

    1.为什么要保证顺序 消息队列中的若干消息如果是对同一个数据进行操作,这些操作具有前后的关系,必须要按前后的顺序执行,否则就会造成数据异常.举例: 比如通过mysql binlog进行两个数据库的数据 ...

  9. VBA事件(十七)

    在VBA中,要手动更改单元格或单元格值范围时,可以触发事件驱动的编程. 更改事件可能会使事情变得更容易,但您可以非常快速地结束一个完整的格式化页面.VBA中有两种事件 - 工作表事件 工作簿事件 工作 ...

  10. RZ70注册SLD

    本文的将S4 abap系统向PO JAVA系统中注册. S4 QASERPAP01 NR=60 ASCS=61 PO QASPISAP01 NR=60 SCS=61 http://qaspisap01 ...