码迷,mamicode.com
首页 > 其他好文 > 详细

[Dart] Mixin

时间:2019-11-05 22:01:25      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:ima   rar   get   span   main   override   print   hang   hiera   

Docs

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();
  }
}

 

[Dart] Mixin

标签:ima   rar   get   span   main   override   print   hang   hiera   

原文地址:https://www.cnblogs.com/Answer1215/p/11801629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!