在 Dart 的可选参数构造函数中使用构造函数初始化成员
Initializing member with constructor in an optional parameter constructor in Dart
根据我的以下代码,我想要 class Hero
的 构造函数 需要 Stats
class作为一个可选参数,它有一个默认值基于它的构造函数 (设置它的健康和攻击领域的那个通过可选的命名参数为 100 和 10) 而不是 null.
void main() {
Hero hero = Hero("Foo");
print('${hero.name} : HP ${hero.stats.health}');
}
class Stats {
Stats({this.health = 100, this.attack = 10});
double health;
double attack;
}
class Hero {
// error: The default value of an optional parameter must be constant
Hero(this.name,[this.stats = Stats()]);
String name;
Stats stats;
}
我尝试过的更多东西:
class Hero {
// error: Can't have a const constructor for a class with non-final fields
Hero(this.name,[this.stats = const Stats()]);
String name;
Stats stats;
}
class Hero {
// error: stats initialized as null
Hero(this.name,[this.stats]);
String name;
Stats stats = Stats();
}
以下代码有效,但它没有将统计信息作为可选参数:
class Hero {
Hero(this.name);
String name;
Stats stats = Stats();
}
(在评论中归功于 @jamesdlin for linking to )
In general, if there isn't a const constructor available, you instead
can resort to using a null default value (or some other appropriate
sentinel value) and then setting the desired value later:
class Foo {
Bar bar;
Foo({Bar bar}) : bar = bar ?? Bar();
}
(Note that explicitly passing null as an argument will do something
different with this approach than if you had set the default value
directly. That is, Foo(bar: null) with this approach will initialize
bar to Bar(), whereas with a normal default value it would initialize
bar to null. In some cases, however, this approach's behavior might be
more desirable.)
根据我的以下代码,我想要 class Hero
的 构造函数 需要 Stats
class作为一个可选参数,它有一个默认值基于它的构造函数 (设置它的健康和攻击领域的那个通过可选的命名参数为 100 和 10) 而不是 null.
void main() {
Hero hero = Hero("Foo");
print('${hero.name} : HP ${hero.stats.health}');
}
class Stats {
Stats({this.health = 100, this.attack = 10});
double health;
double attack;
}
class Hero {
// error: The default value of an optional parameter must be constant
Hero(this.name,[this.stats = Stats()]);
String name;
Stats stats;
}
我尝试过的更多东西:
class Hero {
// error: Can't have a const constructor for a class with non-final fields
Hero(this.name,[this.stats = const Stats()]);
String name;
Stats stats;
}
class Hero {
// error: stats initialized as null
Hero(this.name,[this.stats]);
String name;
Stats stats = Stats();
}
以下代码有效,但它没有将统计信息作为可选参数:
class Hero {
Hero(this.name);
String name;
Stats stats = Stats();
}
(在评论中归功于 @jamesdlin for linking to
In general, if there isn't a const constructor available, you instead can resort to using a null default value (or some other appropriate sentinel value) and then setting the desired value later:
class Foo {
Bar bar;
Foo({Bar bar}) : bar = bar ?? Bar();
}
(Note that explicitly passing null as an argument will do something different with this approach than if you had set the default value directly. That is, Foo(bar: null) with this approach will initialize bar to Bar(), whereas with a normal default value it would initialize bar to null. In some cases, however, this approach's behavior might be more desirable.)