您只能有一个 未命名的 构造函数,但可以有任意数量的其他已 命名的
构造函数
class Player { Player(String name, int color) { this._color = color; this._name = name; } Player.fromPlayer(Player another) { this._color = another.getColor(); this._name = another.getName(); } }new Player.fromPlayer(playerOne);该构造函数可以简化
Player(String name, int color) { this._color = color; this._name = name; }至
Player(this._name, this._color);
命名构造函数也可以通过以
_
class Player { Player._(this._name, this._color); Player._foo();}具有
final字段初始值设定项列表的构造函数是必需的:
class Player { final String name; final String color; Player(this.name, this.color); Player.fromPlayer(Player another) : color = another.color, name = another.name;}


