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 ...
随机推荐
- [转帖]kubernetes ingress 在物理机上的nodePort和hostNetwork两种部署方式解析及比较
kubernetes ingress 在物理机上的nodePort和hostNetwork两种部署方式解析及比较 https://www.cnblogs.com/xuxinkun/p/11052646 ...
- [EF] - "已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭" 之解决
错误 解决 在 ConnectionString 中添加 MultipleActiveResultSets=true(适用于SQL 2005以后的版本).MultipleActiveResultSet ...
- 微信小程序DEMO——面包旅行的代码
API 集合在一起写了一个页面,并导出, const apiURL = 'http://xxx.xxxx.com'; const trip = { hot(data,callback){ wx.req ...
- 天梯赛 L2-023. 图着色问题
题解:用dfs遍历图的每条边就好,这里注意要求颜色的个数为k #include <cstdio> #include <iostream> #include <cstrin ...
- 3.asp.net core 关键概念
1. StartUp类 在Startup.ConfigureServices方法里配置或注册服务 在Startup.Configure方法里配置请求处理管道.请求处理管道由一系列中间件组建构成,每个中 ...
- 字符串replace的理解和练习和配合正则表达式的使用
下面代码展示了(demo地址 https://codepen.io/peach_/pen/jONJjRY): 1.字符串replace的理解和练习和配合正则表达式的使用, 2.正则表达式学习 3.通过 ...
- Lwip与底层的接口
Lwip有三套api,分别是: raw api:使用方法为使用回调函数,即先注册一个函数,当接受到数据之后调用这个函数.缺点是对于数据连续处理不好. Lwip api:把接收与处理放在一个线程里面.因 ...
- node.js 微信开发3-网页授权
1.配置公众号的自定义菜单,如 { "button":[ { "type":"view", "name":"公 ...
- java基础点
1.eclipse什么时候编译java类文件 2.在同一包中的类可以相互引用,无需用import语句 3.在Java eclipse用ALT输入特殊符号 4.if else等语句,什么时候可以不加括号 ...
- linux7 上安装mongodb4.2.1操作步骤
MongoDB是一个通用的.基于文档的分布式数据库,它是为现代应用程序开发人员和云时代而构建的.没有数据库能让你更有效率. 1.下载需要的软件包https://www.mongodb.com/down ...