有下面一个场景:ide
抽象这种场景,对于单继承语言来讲,咱们有接口(interface),来规范,抽象事物共同的行为,约束,规范,Dart 虽然没有 interface 关键字,但也不影响。其实现以下:ui
abstract class Animal {
}
abstract class Run {
void run() => print("会跑");
}
abstract class Fly {
void fly() => print("会飞");
}
abstract class Swim {
void swim() => print("会游泳");
}
class Bird extends Animal implements Fly {
@override
void fly() {
// TODO: implement fly
//super.run();//编译报错,它会认为抽象类 Fly中的fly()方法是抽象的(真实的并非抽象的),
print("会飞");
}
}
class Dog extends Animal implements Run, Swim {
@override
void run() {
// TODO: implement run
print("会跑");
}
@override
void swim() {
// TODO: implement swim
print("会游泳");
}
}
class Person extends Animal implements Run, Swim, Fly {
@override
void fly() {
// TODO: implement fly
print("会飞");
}
@override
void run() {
// TODO: implement run
print("会跑");
}
@override
void swim() {
// TODO: implement swim
print("会游泳");
}
}
复制代码
从上面实现代码来了看,仍是存在问题的,Bird,Dog,PerSon 实现接口的时候都要实现接口里面的方法,但其实能够看到 run(),swim()方法都是同样的。秉着消灭重复代码的原则来讲,接口这种实现仍是有点点问题的。spa
这里提一下 Java8 接口里面的 default 关键字。code
Java8 以前,接口是不能有实体方法的,可是如今能够了,它是这么操做的。继承
public interface Face {
default void HelloWorld(){
System.out.println("Default Hello World ");
}
}
class FaceImpl implements Face {
}
public class Test {
public static void main(String[] args) {
FaceImpl face= new FaceImpl();
face.HelloWorld();
}
}
复制代码
FaceImpl 能够不用实现 HelloWorld(),而能够直接调用它,那 default 有啥用?Java8 以前的接口,每当咱们在扩展接口功能的时候,对应接口的实现类也必须重写实现扩展接口的方法。这时候可能改动就很大。若是这种接口功能的扩展改动就在 SDK 里面,那改动会至关的大。而 default 打破了 Java 以前版本对接口的语法限制(接口里面只能有抽象方法 ),从而使得接口在进行扩展的时候,不会破坏与接口相关的实现类代码。接口
在 dart 中,有能更好的实现上面场景的方式:get
abstract class Animal {}
mixin Run {
void run() => print("会跑");
}
mixin Fly {
void fly() => print("会飞");
}
mixin Swim {
void swim() => print("会游泳");
}
class Bird extends Animal with Fly {
@override
void fly() {
super.fly(); //合情合理合法
}
}
class Dog extends Animal with Run, Swim {
@override
void run() {
super.run();
}
@override
void swim() {
// TODO: implement swim
super.swim();
}
}
class Person extends Animal with Run, Swim, Fly {
@override
void fly() {
super.fly(); //合情合理合法
}
@override
void run() {
super.run();
}
@override
void swim() {
// TODO: implement swim
super.swim();
}
}
void main() {
Person person = new Person();
person.fly();
person.run();
person.swim();
}
复制代码
可见这种实现方式主要的关键字就是 mixin,with。string
mixin 能减小代码冗余,能在多个类层次结构中复用代码,相比约束性很强的接口来讲显得更加自由。it
minxin 是一种特殊的多重继承,适用于如下场景:io
若是在两个 Mixin 模块都有一个同名方法,这两个模块同时混入一个类,那最终调用的会是哪一个模块的方法呢?写个代码跑一下。。。
//Object
class O {
String getMessage() => "O";
}
mixin A {
String getMessage() => 'A';
}
mixin B {
String getMessage() => "B";
}
class AB extends O with A, B {}
class BA extends O with B, A {}
void main() {
String result1 = "";
String result2 = "";
AB ab = AB();
result1= ab.getMessage();
print("ab is ${ab is A}");
print("ab is ${ab is B}");
print("ab is ${ab is O}");
BA ba = BA();
result2 += ba.getMessage();
print("ba is ${ba is B}");
print("ba is ${ba is A}");
print("ba is ${ba is O}");
print(result1);
print(result2);
}
复制代码
ab is true
ab is true
ab is true
ba is true
ba is true
ba is true
B
A
复制代码
由此可知:
咱们限定行走这种功能行为只能在动物身上:
class Animal {}
mixin Walk on Animal {
void walk() {
print("会走路了");
}
}
class ZhiWu with Walk {} //这是错误的写法,编译经过不了
class Person extends Animal with Walk {}
复制代码
能够看出 on 关键字限定了 Walk 这种功能只能在 Animal 的子类混入。
有一种场景,相似人要先学会走路,而后慢慢才会跳舞,也可能一生也不会。。。
class Animal{
}
mixin Walk {
void walk() {
print("会走路了");
}
}
mixin Dance on Animal, Walk {
void dance() {
print("竟然会跳舞了");
}
}
//class Person with Dance {} 错误的写法,编译不经过
//class Person with Walk, Dance {} 错误的写法,编译不经过
class Person extends Animal with Walk, Dance {}
void main() {
Person person = new Person();
person.walk();
person.dance();
}
复制代码
能够看出要想会跳舞,必需是动物,必须学会走路。