Flutter mixins 探究

栏目: Java · 发布时间: 6年前

内容简介:Dart中的定义是:

mixins 作为Flutter的重要特性,值得好好研究一下

0x01 mixins的定义

mixins 的中文意思是混入,就是在类中混入其他功能。

Dart中的定义是:

Mixins are a way of reusing a class’s code in multiple class hierarchies.
复制代码

Mixins是一种在多个类层次结构中重用类代码的方法。

可以看出Mixins最重要的功能是重用代码,我们先看下JAVA,重用代码的方式有哪些:

  1. 继承

    子类可以复用父类的方法和属性,但是 JAVA 里的继承只能单继承。

  2. 组合

    将要重用的代码,封装成类A,让其他类持有A的实例,看上去貌似解决了重用代码的问题,但是一方面,每个类持有的A的实例是不同的,有多少个类,就总共有多少个A的实例,而且另一方面,即使A使用单例,使用起来也很不方便。

  3. 接口 定义一个接口interface,类实现interface,这样虽然接口是同一个,但是实现却是分散的,能重用的代码是有限的。

所以在JAVA里想要重用代码,限制是很多的。

这就有了 mixins 的概念, mixins 最早的根源来自于Lisp,因为Dart也受到smalltakk的影响,所以Dart引入了 mixins 的概念,

在维基百科中有对 mixins 最准确的定义:

在面向对象的语言中,mixins类是一个可以把自己的方法提供给其他类使用,但却不需要成为其他类的父类。
复制代码

mixins 要重用的代码,不是方法或者是接口,而是类!

这里举个例子,有一个类A,A中有一个方法a(),还有一个方法B,也想使用a()方法,那么这时候就需要用到mixins,类A就是mixins类(混入类),类B就是要被mixins的类,对应的Dart代码如下:

class A {
  String content = 'A Class';

  void a(){
    print("a");
  }
}

class B with A{

}

B b = new B();
print(b.content);
b.a();
复制代码

输出是:

A Class
a
复制代码

将类A mixins 到 B,B可以使用A的属性和方法,B就具备了A的功能,但是需要强调的是:

  1. mixins的对象是类

  2. mixins绝不是继承,也不是接口,而是一种全新的特性

0x02 with

mixins要用到的关键字 with

怎么来理解 with 呢?很简单:

继承 -> extends

mixins -> with

继承和mixins是一样的,是语言的特性,with和extends是关键字。

0x03 使用mixins的条件

因为mixins使用的条件,随着Dart版本一直在变,这里讲的是Dart2.1中使用mixins的条件:

on

0x04 一个类可以mixins多个mixins类

看下面代码:

class A {
  void a(){
    print("a");
  }
}

class A1 {
  void a1(){
    print("a1");
  }
}

class B with A,A1{

}

B b = new B();
b.a();
b.a1();
复制代码

输出是:

a
a1
复制代码

但是,如果A和A1的方法相同,而且调换A和A1的顺序,在被mixins的类中实现同一个方法呢,看下面的代码:

class A {
  void a(){
    print("a");
  }
}

class A1 {
  void a(){
    print("a1");
  }
}

class B with A,A1{

}

class B1 with A1,A{

}

class B2 with A,A1{
  void a(){
    print("b2");
  }
}

class C {
  void a(){
    print("a1");
  }
}

class B3 extends C with A,A1{

}

class B4 extends C with A1,A{
  
}

class B5 extends C with A,A1{
  void a(){
    print("b5");
  }
}

B b = new B();
B1 b1 = new B1();
B2 b2 = new B2();
B3 b3 = new B3();
B4 b4 = new B4();
B5 b5 = new B5();



b.a();
b1.a();
b2.a();
b3.a();
b4.a();
b5.a();
复制代码

会是什么样的结果呢?

0x05 mixins的实现原理

Mixins in Dart work by creating a new class that layers the implementation of the mixin
on top of a superclass to create a new class — it is not “on the side” but “on top” of
the superclass, so there is no ambiguity in how to resolve lookups.
复制代码

class B3 extends C with A,A1{

}
复制代码

为例,可以分解为:

class CA = C with A;
class CAA1 = CA with A1;

class B3 extends CAA1{
    
}
复制代码

mixins不是多继承

Mixins is not a way to get multiple inheritance in the classical sense. Mixins is a way to abstract and reuse a family of operations and state. It is similar to the reuse you get from extending a class, but it is compatible with single-inheritance because it is linear.
复制代码

所以输出结果是:

a1
a
b2
a1
a
b5
复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Concepts, Techniques, and Models of Computer Programming

Concepts, Techniques, and Models of Computer Programming

Peter Van Roy、Seif Haridi / The MIT Press / 2004-2-20 / USD 78.00

This innovative text presents computer programming as a unified discipline in a way that is both practical and scientifically sound. The book focuses on techniques of lasting value and explains them p......一起来看看 《Concepts, Techniques, and Models of Computer Programming》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具