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. [转帖]Linux运维工程师的十个基本技能点

    Linux运维工程师的十个基本技能点 https://cloud.tencent.com/developer/article/1115068   本人是Linux运维工程师,对这方面有点心得,现在我说 ...

  2. 5-24 c++语言之【基础知识】

    最近一段时间继续开始了c++的学习,作为c plus plus 难免会与c语言做一个对比,很明显的感受到c++语言注重代码的复用性和拓展性,而c语言更加注重其算法的高效性,这也是今后需要注意的地方,避 ...

  3. shiro与spring集成

    简介 Apache Shiro 是 Java 的一个安全(权限)框架.主要提供了认证.授权.加密和会话管理等功能. Authentication:身份认证/登录,验证用户是不是拥有相应的身份:Auth ...

  4. RabbitMQ集群部署、高可用和持久化

    RabbitMQ 安装和使用 1.安装依赖环境 在 http://www.rabbitmq.com/which-erlang.html 页面查看安装rabbitmq需要安装erlang对应的版本 在 ...

  5. EfCore基本用法

    db first 和 code first的基本使用方法 https://www.cnblogs.com/Starts_2000/p/mysql-efcore20-codefirst-dbfirst- ...

  6. C/C++读写文件的几种方法fstream fopen、fwrite()、fread()操作

    C中采用的主要是文件指针的办法,C++中对文件的操作主要运用了"文件流"(即非标准的输入输出)的思想 c读写文件fopen C 库函数 FILE *fopen(const char ...

  7. docker第二章--数据管理

  8. Hbuilder中配置cmd

    步骤: 1. ①运行——>外部工具——>外部工具 配置2.  新建一个名为cmd(随意命名)的外部工具: 名称:cmd 要执行的命令或文件:C:\Windows\System32\cmd. ...

  9. 正padding负margin实现多列等高布局(转)

    转自: 巧妙运用CSS中的负值 (http://www.webhek.com/post/2345qwerqwer.html) 代码来自: https://codepen.io/Chokcoco/pen ...

  10. python 3.6 + robotFramework自动化框架 环境搭建、学习笔记

    ################################################################# #author: 陈月白 #_blogs: http://www.c ...