【发布时间】:2018-09-16 09:15:39
【问题描述】:
dart中真的不能为一个类创建多个构造函数吗?
在我的 Player 类中,如果我有这个构造函数
Player(String name, int color) {
this._color = color;
this._name = name;
}
然后我尝试添加这个构造函数:
Player(Player another) {
this._color = another.getColor();
this._name = another.getName();
}
我收到以下错误:
默认构造函数已经定义。
我不是通过创建一个带有一堆非必需参数的构造函数来寻找解决方法。
有什么好办法解决这个问题吗?
【问题讨论】:
-
作为不相关的评论,您可能应该对
color和name使用getter,而不是getColor()和getName()方法。如果值永远不会改变,您可以使用单个公共字段,例如class Player { final String name; final int color; Player(this.name, this.color); }。 -
我是飞镖新手,还没有习惯这种标准,但是谢谢,我会试一试的。
-
这也是当您意识到 Java/C# 构造函数重载期间所有蹩脚的初学者所做的... >> “揭开 Java 和 C# 背后的美丽需要时间”!