我知道在C++中我们可以同时拥有两个构造函数。在Dart中,当我尝试编写两个构造函数时,它说“默认的构造函数已定义”
class Human {
double height;
int age;
Human()
{
height = 0;
age = 0;
}
Human (double startingheight){ //The default constructor is already defined
height = startingheight;
}
}
最佳答案
Dart在任何可见的将来均不支持方法/函数重载和will not have it。
您可以在此处执行的操作将参数optional设置为默认值:
作为位置参数:
class Human {
double height = 175;
Human([this.height]);
}
var human1 = Human();
var human = Human(180);
或命名:
class Human {
final double height;
Human({this.height = 175});
}
var human1 = Human();
var human = Human(height: 180);
关于flutter - 如何在Dart/Flutter的同一类中获取默认值和参数化构造函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62407777/