Dart继承和超级构造函数 [英] Dart inheritance and super constructor

查看:2398
本文介绍了Dart继承和超级构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图定义一个抽象父类 Event 和一个具体的子 PlaceChangeEvent

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

PlaceChangeEvent 给我2个编译器警告:

The PlaceChangeEvent is giving me 2 compiler warnings:


  • 第一个是类名本身:遗漏的成员:第二个是调用 super

  • < $ c>: 0位置参数期望,但2找到
  • 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:


  • 名称 c> place.name ;和

  • 使用当前日期/时间硬编码父级的 occurrenceAt 属性;

  • Hardcode the parent's name with the place.name; and
  • Hardcode the parent's occurredAt property with the current date/time'; and
  • Initialize the parent's optional handleCount property to 0 by simply not providing it.

推荐答案

在这种情况下,你正在实现你的类而不是扩展它。实现你的类意味着你的子类必须提供相应的方法。实现意味着你的具体类应该有这些属性的getter和setter(或者只是定义那些属性)。它不会实际继承类中定义的那些,因为你不扩展 Event class

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

这篇关于Dart继承和超级构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆