简体   繁体   English

Dart:继承和超级构造函数

[英]Dart: inheritance and super constructor

My Dart app has the following class hierarchy: 我的Dart应用程序具有以下类层次结构:

abstract class AbstractPresenter {
    AbstractView view;

    AbstractPresenter(this.view);

    void start(EventBus eventBus) {
        view.presenter = this;
        querySelector("body").innerHTML = view.render();
    }
}

abstract class SigninPresenter implements AbstractPresenter {
    void onSignin(MouseEvent mouseEvent);
}

class DefaultSigninPresenter implements SigninPresenter {
    // Lots of code
}

abstract class AbstractView {
    AbstractPresenter presenter;

    AbstractView(this.presenter);

    String render();

    void bindUI(String html);
}

abstract class SigninView implements AbstractView {
    SigninView(AbstractPresenter presenter) : super(presenter);

    LabelElement getSigninLabel();

    void setSigninLabel(LabelElement signinLabel);

    InputElement getEmailTextField();

    void setEmailTextField(InputElement emailTextField);
}

class DefaultSigninView implements SigninView {
    LabelElement signinLabel;
    InputElement emailTextField;

    DefaultSigninView(SigninPresenter presenter) : super(presenter);

    // Lots of code...
}

The idea is to define a hierarchy of "views" and "presenters", where ultimately a DefaultSigninView will get related (bidirectionally) to a DefaultSigninPresenter . 这个想法是定义的“意见”和“主持人”,其中一个最终层次DefaultSigninView将获得相关的(双向)的DefaultSigninPresenter

But I'm getting a compiler warning in DefaultSigninView 's constructor, complaining about my call to super(presenter) : 但我在DefaultSigninView的构造函数中收到编译器警告,抱怨我对super(presenter)调用:

Missing inherited member 'AbstractView.presenter' 缺少继承的成员'AbstractView.presenter'

Why can't it "see" the presenter I'm passing it? 为什么它不能“看到”我传递的主持人呢? The parent constructor ( AbstractView ) takes an AbstractPresenter ... 父构造函数( AbstractView )采用AbstractPresenter ...

This is because SigninView has not implemented all of the members of AbstractView. 这是因为SigninView尚未实现AbstractView的所有成员。 The same situation as your question here: Dart inheritance and super constructor 与你的问题相同的情况: Dart继承和超级构造函数

You are not extending AbstractView but implementing it. 您没有扩展AbstractView,而是实现它。 This means that you must implement a getter/setter for an AbstractPresenter presenter . 这意味着您必须为AbstractPresenter presenter者实现getter / setter。 Properties are not inherited if you only implement the class. 如果仅实现该类,则不会继承属性。

From the Dart language spec : Dart语言规范

A class does not inherit members from its superinterfaces. 类不会从其超接口继承成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM