Define Interfaces and Share Class Members through Mixins in Dart
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的更多相关文章
- sencha touch extend 单继承 和 mixins 实现多继承
继承可以达到代码的复用,利于维护和扩展. sencha touch 中可以通过 extend 实现单继承,通过 mixins 实现多继承. mixins 也很像实现接口,不过这些接口的方法已经实现了, ...
- Generic Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/kwtft8ak(v=vs.140).aspx It is often useful to define interf ...
- [Sencha ExtJS & Touch] 在Sencha(Extjs/Touch)应用程序中使用plugins(插件)和mixins(混入)
原文链接:http://blog.csdn.net/lovelyelfpop/article/details/50853591 英文原文:Using Plugins and Mixins in You ...
- 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 ...
- Dart 基础重点截取 Dart 2 20180417
官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...
- 关于C#你应该知道的2000件事
原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...
- Thinking in Java——笔记(15)
Generics The term "generic" means "pertaining or appropriate to large groups of class ...
- 【转】ASP.NET MVC 的最佳实践
[This post is based on a document authored by Ben Grover (a senior developer at Microsoft). It is ou ...
- [转]UIWebView的Javascript运行时对象
An alternative, that may get you rejected from the app store, is to use WebScriptObject. These APIs ...
随机推荐
- Python进阶:值传递,引用传递?不存在的,是赋值传递
Python的变量及其赋值 c/c++/c#/java 里面都有值传递,引用传递的概念,在Python中是如何的? 例 a = 1 b = a print(id(a),id(b)) #14072334 ...
- (未完成)ARM-linux 移植 SDL
ref : https://blog.csdn.net/u012075739/article/details/24877639 2. 交叉编译SDL 编译SDL前先要编译其依赖库 tsl ...
- 怎样输出Hello World
方法一: 进入python交互模式, 然后使用: print()函数输出 方法二: 新建一个.py文件, 然后写入print()函数, 再使用python命令执行输出:
- TCP(上)
tcp头格式: TCP状态位: SYN表示建立连接, FIN表示关闭连接, ACK表示响应, PSH表示有 DATA数据传输, RST表示连接重置. TCP窗口: TCP 要做流量控制,通信双方各声明 ...
- 2..net core 和.net framework 版本
同一台机器上可以安装多个版本的.net core runtime.比如: 每个.net core项目都可以指定自己所用的版本,所以改变某个项目的target version不会影响到其他的.安装新的r ...
- elementUI表单验证
elementUI表单验证非常方便,我们直奔主题: <template> <el-form ref="orderForm" :model="orderF ...
- 如何使用async和await这对组合设计统一的取Access Token的函数
最近我在使用SAP云平台的机器学习API做和SAP系统的集成,因为SAP Cloud Platform Leonardo上的机器学期API,每次消费时需要传一个Access Token,故在每次实际调 ...
- linux管道和重定向
管道 管道应该是等左边的程序执行完,才使用左边的程序的输出执行右边的程序. 但是在测试的时候,如果左边的程序无限循环且不等待的输出,那么左边的程序执行时右边的程序也会执行,个人感觉这是linux的机制 ...
- echarts的一点记录
echart官网地址: https://www.echartsjs.com/index.html echarts实例地址:https://echarts.baidu.com/examples/ vue ...
- 修改Linux命令的别名:alias
修改Linux命令的别名. (一)临时生效:仅限当前窗口 查看所有别名 alias 添加别名 alias ll='ls -lh' 重置别名 alias ll='ls -alh' 删除别名: unali ...