1
我有一個Field類並且有一個copy()方法,我想將自引用傳遞給新對象,但是Dart編輯器似乎不允許。我如何將自引用傳遞給像java這樣的新對象?我如何將自我引用傳遞給飛鏢中的對象
class Field
Field copy() {
return new Field(this);
}
}
我有一個Field類並且有一個copy()方法,我想將自引用傳遞給新對象,但是Dart編輯器似乎不允許。我如何將自引用傳遞給像java這樣的新對象?我如何將自我引用傳遞給飛鏢中的對象
class Field
Field copy() {
return new Field(this);
}
}
有沒有問題,做這樣的事情:
class Field {
String name;
Field(Field other) {
// init current with other
}
Field copy() => new Field(this);
}
你似乎缺少一個大括號,你沒有一個構造函數。除此之外,沒有理由不這樣做:
class Field {
Field ref;
Field(this.ref);
Field copy() {
return new Field(this);
}
}
Field a = new Field(null);
Field b = a.copy();
print(identical(a, b.ref)); // true