zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    为DataGrid 写一个 DropDownListColumn
    网页向女友告白和纪念日专用特效
    CentOS6.5 64位站点压力測试工具webbench
    RxJava系列之二 变换类操作符具体解释1
    C语言实现单链表节点的删除(带头结点)
    excel 补全全部空格
    Linux经常使用命令-文件搜索命令-文件搜索命令find
    错误代码: 1045 Access denied for user 'skyusers'@'%' (using password: YES)
    Yocto tips (17): Yocto License问题:restricted license not whitelisted in LICENSE_FLAGS_WHITELIST
    二叉树的基本使用
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11409024.html
Copyright © 2011-2022 走看看