[英]Dart Multiple Constructors
真的不可能在 dart 中為一個類創建多個構造函數嗎?
在我的播放器類中,如果我有這個構造函數
Player(String name, int color) {
this._color = color;
this._name = name;
}
然后我嘗試添加這個構造函數:
Player(Player another) {
this._color = another.getColor();
this._name = another.getName();
}
我收到以下錯誤:
默認構造函數已經定義。
我不是通過創建一個帶有一堆非必需參數的構造函數來尋找解決方法。
有沒有很好的方法來解決這個問題?
只能有一個未命名的構造函數,但可以有任意數量的附加命名構造函數
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;
}
如果您的班級使用最終參數,則接受的答案將不起作用。 這樣做:
class Player {
final String name;
final String color;
Player(this.name, this.color);
Player.fromPlayer(Player another) :
color = another.color,
name = another.name;
}
如果您已經在項目中使用了帶參數的構造函數,現在您發現需要一些無參數的默認構造函數,則可以添加一個空的構造函數。
class User{
String name;
User({this.name}); //This you already had before
User.empty(); //Add this later
}
class MyClass {
//These two are private attributes
int _age;
String _name;
//This is a public attribute
String defaultName = "My Default Name!";
//Default constructor
MyClass() {
_age = 0;
_name = "Anonymous";
}
MyClass.copyContructor(MyClass fromMyClass) {
this._age = fromMyClass._age;
this._name = fromMyClass._name;
}
MyClass.overloadedContructor(String name, int age) {
this._age = age;
this._name = name;
}
MyClass.overloadedContructorNamedArguemnts({String name, int age}) {
this._age = age;
this._name = name;
}
//Overriding the toString() method
String toString() {
String retVal = "Name:: " + _name + " | " + "Age:: " + _age.toString();
return retVal;
}
}
//The execution starts from here..
void main() {
MyClass myClass1 = new MyClass();
//Cannot access oprivate attributes
//print(myClass1.name);
//print(myClass1.age);
//Can access the public attribute
print("Default Name:: " + myClass1.defaultName);
print(myClass1.toString());
MyClass myClass2 = new MyClass.copyContructor(myClass1);
print(myClass2.toString());
MyClass myClass3 = new MyClass.overloadedContructor("Amit", 42);
print(myClass3.toString());
MyClass myClass4 =
new MyClass.overloadedContructorNamedArguemnts(age: 42, name: "Amit");
print(myClass4.toString());
}
您可以使用工廠構造函數
factory Player.fromPlayer(Player another) => Player(another.name, another.color);
我找到了解決此問題的解決方案,這取決於檢查傳遞給函數的數據類型
試試這個解決方案
Dart 不支持參數重載(具有多個同名但參數不同的函數)。 這也適用於構造函數——這就是為什么在 SDK 中有這么多帶有命名構造函數的類的原因。
在 Dart 中,您可以使用默認構造函數、命名構造函數、工廠方法和靜態方法來實例化類:
class A {
// Default constructor
A() : msg = '1';
// Named constructor with positional param
A.message(this.msg);
// Factory method with named param
factory A.underscore({String msg = ''}) {
return A.message('_'+msg);
}
// Factory method with arrow func body
static A bang(msg) => A.message('!'+msg);
final String msg;
}
void main() {
print(A().msg);
print(A.message('2').msg);
print(A.underscore(msg: '3').msg);
print(A.bang('4').msg);
}
輸出:
1
2
_3
!4
Class User{ User(); User.fromName(this.name); String? name; }
正如Günter Zöchbauer在他的回答中已經指出的那樣:
你只能有一個未命名的構造函數,但在 Flutter 中你可以有任意數量的額外命名構造函數。
命名構造函數的語法:
class_name.constructor_name (arguments) {
// If there is a block of code, use this syntax
// Statements
}
or
class_name.constructor_name (arguments);
// If there is no block of code, use this syntax
更多見解請 點擊這里
要了解 Flutter 中的各種構造函數,請單擊此處
真的不可能在dart中為一個類創建多個構造函數嗎?
在我的播放器類中,如果我有此構造函數
Player(String name, int color) {
this._color = color;
this._name = name;
}
然后,我嘗試添加此構造函數:
Player(Player another) {
this._color = another.getColor();
this._name = another.getName();
}
我收到以下錯誤:
默認構造函數已經定義。
我不是通過創建帶有一堆不需要的參數的構造函數來尋找解決方法。
有解決這個問題的好方法嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.