I am trying to define an abstract parent class Event and a concrete child PlaceChangeEvent:
abstract class Event {
String name;
DateTime occurredAt;
int handleCount;
Event(this.name, this.occurredAt, [this.handleCount = 0]);
}
class PlaceChangeEvent implements Event {
Place place;
PlaceChangeEvent(Place place) : super(place.name, DateTime.now());
}
The PlaceChangeEvent is giving me 2 compiler warnings:
- The first is on the class name itself:
Missing inherited members: 'Event.name', 'Event.handleCount' and 'Event.occurredAt' - The second is on the call to
super:0 positional arguments expected, but 2 found
Where did I go wrong and what can I do to fix this? My intentions are to:
- Hardcode the parent's
namewith theplace.name; and - Hardcode the parent's
occurredAtproperty with the current date/time'; and - Initialize the parent's optional
handleCountproperty to 0 by simply not providing it.
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'
来源:https://stackoverflow.com/questions/20859472/dart-inheritance-and-super-constructor