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 ...
随机推荐
- 使用RestTemplate进行服务调用的几种方式
首先我们在名为MSG的服务中定义一个简单的方法 @RestController public class ServerController { @GetMapping("/msg" ...
- 往List集合循环add(对象)得到的是重复对象
记录每次的错误,强大是慢慢的积累,先看看代码, 往list中循环添加RoleKungFu对象,看似没有问题,结果打印则显示: 全部是重复的数据!这是因为啥呢,因为将对象add入list中时,放入lis ...
- Vue Prop属性(父to子)
通过Prop向子组件传递数据 第一步父组件中 <template> <div id="app"> <Users :users="users& ...
- C# EF 加密连接数据库连接字符串
不多说,直接上代码 public partial class Model1 : DbContext { private static string connStr = ""; pu ...
- Java源码阅读之ArrayList
基于jdk1.8的ArrayList源码分析. 实现List接口最常见的大概就四种,ArrayList, LinkedList, Vector, Stack实现,今天就着重看一下ArrayList的源 ...
- 一种电平转换的方法,使用CPLD
参考应用笔记 http://www.doc88.com/p-0197252336968.html 前言 在原理图设计初期,可能涉及到引脚电平的转换操作,比如主FPGA的某BANK电平为1.5V,但外围 ...
- 2602978 - [How to] Content Synchronization between SLDs
http://47.101.174.212:52000/sld http://47.101.176.136:56000/sld Symptom As described in Planning Gui ...
- [LeetCode] 234. 回文链表 ☆(翻转链表)
描述 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2输出: false示例 2: 输入: 1->2->2->1输出: true 进阶:你能否用 O(n) 时间复杂 ...
- Linux-开机启动程序
尝试一下几种方法: 1.修改 /etc/rc.local文件. 在exit0 前添加启动命令 2.在/home/pi/.config/autostart/ 下添加.desktop 在.config ...
- Android笔记(二十五) ListView的缓存机制与BaseAdapter
之前接触了ListView和Adapter,Adapter将数据源和View连接起来,实际应用中,我们要显示的数据往往有很多,而屏幕只有那么大,系统只能屏幕所能显示的内容,当我们滑动屏幕,会将旧的内容 ...