Flutter 继承、多继承

2022-05-06  本文已影响0人  雨泽Sunshine

一.继承extends

二.抽象类abstract

abstract class Example {
  /// getter属性
  int get calculate;

  /// 抽象方法
  void methodOne();
  
  /// 普通方法
  void methodTwo() {
    print('methodTwo');
  }
}

三.接口实现implements

class AnotherExample implements Example {
  @override
  int get calculate => 1;
  
  @override
  void methodOne() {
    print('methodOne');
  }
  
  @override
  void methodTwo() {
    print('methodTwo');
  }
}
abstract class InterfaceOne {
  void one();
}

class InterfaceTwo {
  void two() {}
}

class Interface implements InterfaceOne, InterfaceTwo {
  @override
  void one() {}

  @override
  void two() {}
}

四.混合mixin

mixin Breathing {
  void swim() => print('Breathing');
}

mixin Walking {
  void walk() => print('Walking');
}

mixin Coding {
  void code() => print('Hello world');
}

abstract class Human with Breathing {
  
}

class Developer extends Human with Walking, Coding {
  
}

void main() {
  final developer = Developer();
  developer..walk()..code()..swim();
}
mixin BallUtil in Widget {
  double ballVolume(double radius) {
    return 4 / 3 * 3.14 * pow(radius, 3);
  }
}

class VolleyballPitch extends StatelessWidget with BallUtils {
  // code...
} 
上一篇 下一篇

猜你喜欢

热点阅读