简体   繁体   English

命名继承的构造函数,不在dart中接受参数

[英]Named inherited constructor not taking arguments in dart

I am trying to inherit a dart class with a named constructor and I want to pass data into the parent class using super and the named constructor through the child class. 我试图继承一个带有命名构造函数的dart类,我想通过子类使用super和命名构造函数将数据传递给父类。

I have tried many variations, but this should work according to the documentation. 我尝试了很多变化,但这应该根据文档工作。 I am using Dart 2.0. 我正在使用Dart 2.0。 Note: if I have one argument and not a named constructor it works. 注意:如果我有一个参数而不是命名构造函数,它可以工作。

class Person {

  String firstname;
  String lastname;
  String city;

//empty constructor
  Person();

//named constructors
  Person.setfirstlast(String firstname, String lastname);

//setters and getters ...
}

class Worker extends Person{

  String company;
  String title;
  double salary;
  String _memo;

  //empty constructor
  Worker(){
    _memo = "Great Worker";
  }

  //create named constructor  - WHY WILL SUPER.setfirstlast NOT WORK?

  Worker.setWorkerInfo( this.company, this.title, this.salary) :
     super.setfirstlast(firstname, lastname)
  {
      _memo = "Great Worker"; 
  } 
  //setters and getters ...
}

Here is the error message. 这是错误消息。

String firstname Only static members can be accessed in 字符串firstname只能访问静态成员

initializers.dart(implicit_this_reference_in_initializer)

String lastname Only static members can be accessed in String lastname只能访问静态成员

initializers.dart(implicit_this_reference_in_initializer)

Thanks in advance! 提前致谢!


** The Answer ** ** 答案 **

For those interested, I found the solution and this is how you code it. 对于那些感兴趣的人,我找到了解决方案,这就是你编码的方式。 Dart has states that constructors and not inherited, but also includes super. Dart声明构造函数而不是继承,但也包括super。

The 1 minute explanation is that you essentially create a new constructor in the child which includes the fields from the base class. 1分钟的解释是,您实际上在子项中创建了一个新的构造函数,其中包含基类中的字段。 Note, you use "this" for the child fields and you use the data type and placeholders for the inherited fields in the actual constructor. 注意,您对子字段使用“this”,并在实际构造函数中使用数据类型和占位符作为继承字段。 In this case, String fn and String ln. 在这种情况下,String fn和String ln。 The place holders are then brought over via super. 占位符然后通过超级带来。

  Worker.setWorkerInfo( this.company, this.title, this.salary, String fn, String ln) :
     super.setfirstlast(fn, ln)
  {
      _memo = "Great Worker"; 
  } 

The super.setfirstlast(firstname, lastname) references firstname and lastname . super.setfirstlast(firstname, lastname)引用firstnamelastname Those are not parameters to the Worker.setWorkerInfo constructor, and they are not declared in the surrounding lexical scope, so they instead implicitly refer to this.firstname and this.lastname . 这些不是 Worker.setWorkerInfo构造函数的参数,并且它们不在周围的词法范围中声明,因此它们隐式引用this.firstnamethis.lastname

You cannot refer to this in a constructor initializer list, which is what the error is trying to tell you ... just not very successfully, since it doesn't tell you what you are doing, only what you must do instead. 你不能在构造函数初始化列表中引用this ,这是错误试图告诉你的......只是不是很成功,因为它不会告诉你你在做什么,只是你必须做的事情。

You likely want to add String firstname, String lastname as parameters to the Worker.setWorkerInfo constructor. 您可能希望将String firstname, String lastname作为参数添加到Worker.setWorkerInfo构造函数中。

It has no bearing on the problem that the constructor happens to be named. 它与构造函数碰巧被命名的问题无关。 The same problem would occur for an unnamed constructor. 对于未命名的构造函数,也会出现同样的问题。

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

相关问题 Dart 中的构造函数的命名为 arguments 的 inheritance - inheritance of named arguments of constructor in Dart 将继承的类构造函数参数传递给基本构造函数参数 - Pass Inherited Class Constructor Arguments to Base Constructor Arguments 为什么继承的构造函数不能继承默认参数? - Why shouldn't the inherited constructor inherit the default arguments? 通过派生构造函数的参数初始化继承的类(C#) - Initializing Inherited Class Through Derived Constructor's Arguments (C#) 'this'在构造函数和继承的构造函数中是不一样的 - 'This' is not the same in constructor and inherited constructor C ++没有合适的默认构造函数-没有参数的继承的模板构造函数 - C++ no appropriate default constructor available - inherited templated constructor w/ no arguments 构造函数在Java中调用继承的构造函数 - Constructor calling inherited constructor in java Python 3:当子构造函数比父构造函数具有更多参数时,从继承的方法返回新的子类实例 - Python 3: Returning a new child class instance from an inherited method, when child constructor has more arguments than parent constructor 删除构造函数是继承的吗? - Is the deletion of a constructor inherited? 继承类的构造函数的格式 - Format of constructor of an inherited class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM