[Dart] Mixin
Mixins are a way of reusing a class’s code in multiple class hierarchies.
void main() {
Animal animal = Animal();
Bird().fly();
Fish().swim();
Duck().move();
Duck().swim();
Duck().fly();
}
mixin CanSwim {
void swim() {
print(‘change poisiton by swimming‘);
}
}
mixin CanFly {
void fly() {
print(‘change poisiton by flying‘);
}
}
class Animal {
void move () {
print(‘change position‘);
}
}
class Fish extends Animal with CanSwim{
@override
void move () {
super.move();
}
}
class Bird extends Animal with CanFly{
@override
void move () {
super.move();
}
}
class Duck extends Animal with CanFly, CanSwim{
@override
void move () {
super.move();
}
} 相关推荐
万物weiyi 2020-06-16
Samlss 2020-06-04
liutong 2020-05-12
万物weiyi 2020-03-04
mryangjx 2020-03-01
liutong 2020-02-26
liutong 2020-02-22
Samlss 2020-02-22
chaoxiao 2020-02-15
mryangjx 2020-01-28
mryangjx 2020-01-25
liutong 2020-01-12
apowerfulman 2020-01-07
Samlss 2020-01-07
万物weiyi 2020-01-01
mryangjx 2019-12-25
Samlss 2019-12-15