【发布时间】:2020-07-12 17:40:04
【问题描述】:
基本上我想将随机参数两次传递给父类。
考虑以下示例:
import 'dart:math' as math;
class A {
double a;
double b;
A(this.a, this.b) {
if (a != b) {
print('Error');
} else {
print('Alright');
}
}
}
class B extends A {
B() : super(math.Random().nextDouble(), math.Random().nextDouble());
}
main() {
B();
}
这是我能想到的唯一解决方案,但这感觉太老套了……我希望其他人可以为我提供更好的解决方案。
import 'dart:math' as math;
class A {
double a;
double b;
A(this.a, this.b) {
if (a != b) {
print('Error');
} else {
print('Alright');
}
}
}
class B extends A {
static List<double> _c = [0];
bool uselessBool;
B() : uselessBool = random(),
super(_c[0], _c[0]);
static bool random() {
_c[0] = math.Random().nextDouble();
return true;
}
}
main() {
B();
}
我可能存在的东西是这样的(例如你在 python 中所做的):
import 'dart:math' as math;
class A {
double a;
double b;
A(this.a, this.b) {
if (a != b) {
print('Error');
} else {
print('Alright');
}
}
}
class B extends A {
B() {
var c = math.Random().nextDouble();
super(c, c);
}
}
main() {
B();
}
【问题讨论】:
-
我认为最好的方法是为 A 创建一个单参数构造函数。
-
我没有创建 A 类。在这个例子中我做了,但它是一个更大问题的简化。
标签: class inheritance dart super