我有以下構件字段和構造一個類SomeClass
甲包含相同類的另一個構造的構造/對象
private int someInt;
private String someStr;
private String strTwo;
//the contructors
public SomeClass() {}
// second constructor
public SomeClass(int someInt, String someStr) {
this.someInt = someInt;
this.someStr = someStr;
}
// my emphasis here
public SomeClass(int someInt, String someStr, String strTwo) {
// can i do this
new SomeClass(someInt, someStr); // that is, calling the former constructor
this.strTwo = strTwo;
}
將在第三構造創建相同的對象爲:
public SomeClass(int someInt, String someStr, String strTwo) {
this.someInt = someInt;
this.someStr = someStr;
this.strTwo = strTwo;
}
調用另一個構造函數完成這個(...)關鍵字 – nitegazer2003
你是什麼意思'self.someInt'?它應該是'this.someInt'我想 –