【发布时间】:2015-04-27 17:15:54
【问题描述】:
我有一个类 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;
}
【问题讨论】:
-
使用 this(...) 关键字调用另一个构造函数
-
'self.someInt' 是什么意思?我想应该是“this.someInt”
标签: java multiple-constructors