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程序设计基本方法图
 - str.format() 格式化数字的多种方法
			
Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前的 % . format 函数可以接受不限个参数 ...
 - Python爬虫框架
			
本文章的源代码来源于https://github.com/Holit/Web-Crawler-Framwork 一.爬虫框架的代码 import urllib.request from bs4 imp ...
 - go http简单的表单处理
			
//表单处理 package main import ( "net/http" "io" "fmt" &qu ...
 - jquery封装的方法
			
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
 - python读取文件行数和某行内容
			
学习记录: python计算文件的行数和读取某一行内容的实现方法 - nkwy2012 - 博客园https://www.cnblogs.com/nkwy2012/p/6023710.html 文本文 ...
 - 持久化存储之 PV、PVC、StorageClass
			
PV介绍: PersistentVolume(PV)是群集中由管理员配置的一块存储. 它是集群中的资源,就像节点是集群资源一样. PV是容量插件,如Volumes,但其生命周期独立于使用PV的任何单个 ...
 - C#简单工厂案例
			
using System; namespace Application { class JianDanGongChang { static void Main(string[] args) { Fac ...
 - CentOS7安装CDH 第八章:CDH中对服务和机器的添加与删除操作
			
相关文章链接 CentOS7安装CDH 第一章:CentOS7系统安装 CentOS7安装CDH 第二章:CentOS7各个软件安装和启动 CentOS7安装CDH 第三章:CDH中的问题和解决方法 ...
 - Android笔记(十四) Android中的基本组件——按钮
			
Android中的按钮主要包括Button和ImageButton两种,Button继承自TextView,而ImageButton继承自ImageView.Button生成的按钮上显示文字,而Ima ...