[英]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)
引用firstname
和lastname
。 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.firstname
和this.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.