加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
08_inheritance.dart 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
ryanpenn 提交于 2019-03-27 10:55 . dart programming
///
/// inheritance.dart
///
main(List<String> args) {
Animal animal = Dog('black');
animal.eat();
(animal as Dog).bark();
if (animal is Duck) {
print('Yes');
} else {
print('No');
}
Duck duck = Duck('white');
duck.eat();
print(duck is Animal);
YellowFlyDuck yellowFlyDuck = YellowFlyDuck();
yellowFlyDuck.eat();
yellowFlyDuck.flyInSky();
yellowFlyDuck.count();
yellowFlyDuck.output();
print(Duck.type);
// print(YellowFlyDuck.type);
}
class Animal {
String color;
eat() {
print('Eat!');
}
}
class Dog extends Animal {
Dog(String color) {
super.color = color;
}
@override
eat() {
print('$color dog eats meat.');
}
void bark() {
print("Bark !");
}
}
class Duck extends Animal {
static String type = "DUCK";
Duck(String color) {
super.color = color;
}
@override
eat() {
print('$color duck eats rice.');
}
}
abstract class Fly {
void flyInSky();
}
class CountableMixin {
int _count = 0;
void count() {
_count++;
}
output() {
print('count: $_count');
}
}
class YellowFlyDuck extends Duck with CountableMixin implements Fly {
YellowFlyDuck() : super('Yellow');
@override
void flyInSky() {
print('$color duck Fly!');
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化