没有没有参数的无名称构造函数

huangapple go评论115阅读模式
英文:

no unnamed constructor that takes no arguments

问题

我目前正在学习Dart,并尝试使用类、mixin和构造函数进行实验。我遇到了一个似乎无法解决的问题。

导致问题的代码如下:

class Spacecraft {
  String name;

  DateTime? launchDate;
  int? get launchYear => launchDate?.year;

  Spacecraft(this.name, this.launchDate);
  Spacecraft.unlaunched(String name) : this(name, null);

  void describe() {
    print('Spacecraft: $name');
    var launchDate = this.launchDate;

    if (launchDate != null) {
      
      int years = DateTime.now().difference(launchDate).inDays ~/ 365;
      print('Launched: $launchYear ($years years ago)');
    } else {
      print('Unlaunched');
    }
  }
}
mixin Piloted {
  int astronauts = 1;

  void describeCrew() {
    print('Number of astronauts: $astronauts');
  }
}

class PilotedCraft extends Spacecraft with Piloted {

  PilotedCraft(super.name, super.launchDate, super.astronauts);
  @override
  void describe() {
    super.describe();
    super.describeCrew();
  }
}

void main() {
  var spacecraft = PilotedCraft('ISS', DateTime(2010, 11, 20), 6);
  spacecraft.describe();
}

当我尝试运行这段代码时,我收到以下错误消息:

00-Introduction-mixin.dart:20:3: Error: The superclass, 'Spacecraft with Piloted', has no unnamed constructor that takes no arguments.
  PilotedCraft(super.name, super.launchDate, super.astronauts);

我理解这个错误与我尝试创建PilotedCraft类的新实例的方式有关,但我不确定正确的方法应该是什么。

我尝试阅读了Dart关于构造函数和mixin的文档,但仍然无法弄清楚出了什么问题。

有人能解释一下是什么原因导致了这个错误,以及我如何修改我的代码以解决这个问题吗?

非常感谢您的帮助。

英文:

I'm currently learning Dart and experimenting with classes, mixins, and constructors. I've run into an issue that I can't seem to resolve.

Here's the code that's causing the problem:

class Spacecraft {
  String name;

  DateTime? launchDate;
  int? get launchYear => launchDate?.year;

  Spacecraft(this.name, this.launchDate);
  Spacecraft.unlaunched(String name) : this(name, null);

  void describe() {
    print('Spacecraft: $name');
    var launchDate = this.launchDate;

    if (launchDate != null) {
      
      int years = DateTime.now().difference(launchDate).inDays ~/ 365;
      print('Launched: $launchYear ($years years ago)');
    } else {
      print('Unlaunched');
    }
  }
}
mixin Piloted {
  int astronauts = 1;

  void describeCrew() {
    print('Number of astronauts: $astronauts');
  }
}

class PilotedCraft extends Spacecraft with Piloted {

  PilotedCraft(super.name, super.launchDate, super.astronauts);
  @override
  void describe() {
    super.describe();
    super.describeCrew();
  }
}

void main() {
  var spacecraft = PilotedCraft('ISS', DateTime(2010, 11, 20), 6);
  spacecraft.describe();
}

When I attempt to run this, I receive the following error:

00-Introduction-mixin.dart:20:3: Error: The superclass, 'Spacecraft with Piloted', has no unnamed constructor that takes no arguments.
  PilotedCraft(super.name, super.launchDate, super.astronauts);

I understand that this error is related to the way I'm trying to create a new instance of the PilotedCraft class, but I'm not sure what the correct approach should be.

I've tried reading the Dart documentation on constructors and mixins, but I still can't seem to figure out what's going wrong.

Could someone explain what's causing this error and how I can modify my code to resolve this issue?

Thank you in advance for your help.

答案1

得分: 2

当你执行以下代码时:

PilotedCraft(super.name, super.launchDate, super.astronauts);

你正在使用super-initializer parameters。这要求基类(Spacecraft with Piloted)有一个具有相同名称(在本例中是无名称)和相同参数的构造函数。然而,基类Spacecraft中的无名称构造函数没有astronauts参数:

Spacecraft(this.name, this.launchDate);

我不知道为什么你会收到关于“一个没有参数的无名称构造函数”的错误消息;这可能是一个错误。对我来说,在Dart 3.0.2分析器(dart analyze)生成了以下错误:

No associated positional super constructor parameter.

Try using a normal parameter, or adding more positional parameters to the super constructor.

正如分析器所解释的,要修复这个问题,你需要将PilotedCraftastronauts作为普通参数,并单独处理它:

PilotedCraft(super.name, super.launchDate, int astronauts) {
  this.astronauts = astronauts;
}
英文:

When you do:

> dart
> PilotedCraft(super.name, super.launchDate, super.astronauts);
>

you're using super-initializer parameters. This expects that the base class (Spacecraft with Piloted) has a constructor with the same name (in this case, no name) that has the same parameters. However, the unnamed constructor in the base Spacecraft class has no astronauts parameter:

> dart
> Spacecraft(this.name, this.launchDate);
>

I don't know why you're getting an error about "an unnamed constructor that takes no arguments"; that's possibly a bug. For me, the Dart 3.0.2 analyzer (dart analyze) generates an error:

> No associated positional super constructor parameter.
>
> Try using a normal parameter, or adding more positional parameters to the super constructor.

As explained by the analyzer, to fix this, you will need to make PilotedCraft take astronauts as a normal parameter and handle it separately:

PilotedCraft(super.name, super.launchDate, int astronauts) {
  this.astronauts = astronauts;
}

huangapple
  • 本文由 发表于 2023年6月2日 10:42:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定