on 在 mixin 中的使用
abstract class Animal {
// properties
String name;
double speed;
// constructor
Animal(this.name, this.speed);
// abstract method
void run();
}
// mixin CanRun is only used by class that extends Animal
// 其实就是把 mixin CanRun 依附于 Animal 上面(只有在有Animal的时候才能 with Canrun)
mixin CanRun on Animal {
// implementation of abstract method
@override
void run() => print('$name is Running at speed $speed');
}
class Dog extends Animal with CanRun {
// constructor
Dog(String name, double speed) : super(name, speed);
}
void main() {
var dog = Dog('My Dog', 25);
dog.run();
}
// Not Possible
// class Bird with Animal { }
on 在捕获异常中的使用
Future<void> main() async {
AnimalAction dog = Dog('sDog');
dog.crow();
/// 直接申明了一个变量,用来存储
dynamic res;
try {
res = 1 / 0;
} on Exception catch (e) {
print(e); // 不执行
} finally {
print(res);
}
try {
throw Exception('my err');
} on Exception {
stderr.write('null err');
await stderr.flush();
await stderr.close();
}
}