Dart inheritance and super constructor

倖福魔咒の 提交于 2019-12-06 11:09:15

You may want to extend from your Event class instead of implementing it:

class PlaceChangeEvent extends Event {

Implements mean, that your class implements the interface of the class, but not inherits its members, you need to implement them by your self. You get the warning about the super constructor as you still extend from Object instead of Event and Object don't have a constructor with two arguments.

In this case, you're implementing your class rather than extending it. Implementation of your class means that your sub-class must provide the corresponding methods. The implementation means that your concrete class should have a getter and setter for those properties (or just define those properties themselves). It won't actually inherit the ones defined in the class because you are not extending the Event class

I know this question is old and provided answer is good, should be accepted. So, here an example on difference between implements and extends:

import 'dart:math';

abstract class Shape {
  num get area;

  @override String toString() => 'Area is ${this.area}';
}

class Square extends Shape {
  final num side;
  Square({this.side: 2});

  num get area => this.side * this.side;
}

class Circle implements Shape {
  final num radius;
  Circle(this.radius);
  num get area => PI * pow(radius, 2);
}

void main() {
  print(new Square());
  print(new Circle(4));
}

And when you run it:

dart "./dart-tut/shape.dart"
Area is 4
Instance of 'Circle'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!