有没有更好的方法通过在 Dart 中添加 2 个对象键来创建对象?
Is there a better way to create an object from addition of 2 objects keys in Dart?
我制作了一个只有 int 属性的 Stats class。我想创建一种方法来添加两个 Stats 实例以创建一个新实例。我所做的如下:
class Stats {
final int movement;
final int strength;
final int constitution;
Stats(
{this.movement,
this.strength,
this.constitution});
Stats add(Stats other) {
return Stats(
movement: this.movement + other.movement,
strength: this.strength + other.strength,
constitution: this.constitution + other.constitution);
}
}
有没有一种聪明的方法可以通过使用键到键的映射来做到这一点?
没有
Dart 对象属性不是您可以抽象的东西,至少在不使用 dart:mirrors
的情况下不能(然后它真的不再是 聪明 )。
我制作了一个只有 int 属性的 Stats class。我想创建一种方法来添加两个 Stats 实例以创建一个新实例。我所做的如下:
class Stats {
final int movement;
final int strength;
final int constitution;
Stats(
{this.movement,
this.strength,
this.constitution});
Stats add(Stats other) {
return Stats(
movement: this.movement + other.movement,
strength: this.strength + other.strength,
constitution: this.constitution + other.constitution);
}
}
有没有一种聪明的方法可以通过使用键到键的映射来做到这一点?
没有
Dart 对象属性不是您可以抽象的东西,至少在不使用 dart:mirrors
的情况下不能(然后它真的不再是 聪明 )。