我了解Dart语法就是参照上面链接文章
重点理解mixin
mixin,翻译过来就是混入,个人理解是可以在原来类的基础上插入新的类的方法及实现,原来的类可以直接调用mixin类的方法
场景:
java里面的实现如下:
interface CanFixComputer { void fixComputer();}
interface CanDesignSoftware { void designSoftware();}
//软件工程师
class SoftwareEngineer extends Engineer implements CanFixComputer, CanDesignSoftware {
@Override public void fixComputer() { System.out.println("修电脑"); }
@Override public void designSoftware() { System.out.println("设计软件"); }
}
//IT教师class ITTeacher extends Teacher implements CanFixComputer {
@Override public void fixComputer() { System.out.println("修电脑"); }
}
dart里面的实现如下:
abstract class CanFixComputer { void fixComputer();}
abstract class CanDesignSoftware { void designSoftware();}
//软件工程师
class SoftwareEngineer extends Engineer implements CanFixComputer, CanDesignSoftware {
void fixComputer() { print('修电脑'); }
void designSoftware() { print('设计软件'); }
}
//IT教师
class ITTeacher extends Teacher implements CanFixComputer {
void fixComputer() { print('修电脑'); }
}
还是上面的例子,在Dart里面,如果运用Mixin思想的呈现:
abstract class CanFixComputer {
factory CanFixComputer._() { return null; }
void fixComputer() { print('修电脑'); }
}
abstract class CanDesignSoftware {
factory CanDesignSoftware._() { return null; }
void designSoftware() { print('设计软件'); }
}
//软件工程师
class SoftwareEngineer extends Engineer with CanFixComputer, CanDesignSoftware {}
//IT教师
class ITTeacher extends Teacher with CanFixComputer {}
使用情况:
ITTeacher itTeacher = new ITTeacher();
itTeacher.doWork();
itTeacher.fixComputer();
SoftwareEngineer softwareEngineer = new SoftwareEngineer();
softwareEngineer.doWork();
softwareEngineer.fixComputer();
softwareEngineer.designSoftware();
这里关键字从 implements 变成了 with,而且,每个具有某项特性的类不再需要具体去实现同样的功能,Dart的Mixin机制就比Java的接口会高效,开发上层的只需要关心当前需要什么特性,而开发功能模块的关心具体要实现什么功能
extends,with,implements 顺序的理解
class First {
void doPrint() { print('First'); }
}
class Second {
void doPrint() { print('Second'); }
}
class Father {
void doPrint() { print('Father'); }
}
class Son1 extends Father with First,Second {
}
class Son2 extends Father with First implements Second {
}
class Son3 extends Father with First implements Second {
void doPrint() { print('Son'); }
}
main() {
Son1 son1 = new Son1();
son1.doPrint();
Son2 son2 = new Son2();
son2.doPrint();
Son3 son3 = new Son3(); son3.doPrint();
}
控制台输出:
Second
First
Son
implements 只说要实现某个方法,自身不负责实现
所以可以看出,优先级:在自身类的实现 >mixin(如果存在冲突,后面的覆盖前面的)>父类
多重继承
当只使用mixin时,mixin类的声明最好使用mixin关键字
mixin First on Father {
void doPrint() { print('Father'); }
}
当使用on关键字,自身不能作为父接口,表示该mixin只能在它的子类使用
class Son extends Father with First {
}
多重继承场景:
mixin First on Father {
void init() {
print('First init');
super.init();
print('First end');
}
}
mixin Second on Father {
void init() {
print('Second init');
super.init();
print('Second end');
}
}
class Father {
void init() {
print('Father init');
}
Father() {
init();
}
}
class Son extends Father with First,Second {
@override
void init() {
print('Son init');
super.init();
print('Son end');
}
}
控制台输出:
Son init
Second init
First init
Father init
First end
Second end
Son end
学习参考链接:kevinWu的博客
OVER,THANKS