class Person {
String name;//加static
String department;
int age;//加static
int num = age;
public Person()
{
System.out.println(name+"::"+age);
}
public Person(String n) {
name = n;
}
public Person(String n, int a) {
name = n;
age = a;
}
public Person(String n, String d, int a) {
// 完成Person(String n, int a)的逻辑
this(name,age); //【【【此处调用出错】】】
department = d;
}
}
对象依靠构造函数,实例成员依靠对象,所以会报错。构造函数没完成,对象建立就没完成
Within a class, the 'this' Java keyword refers to the native object, the current instance of the class. Within a constructor, you can use the thiskeyword in 3 different ways:
- on the first line, you can call another constructor, using this(...);
- as a qualifier when referencing fields, as in this.fName
- as a reference passed to a method of some other object, as in blah.operation(this);
You can get into trouble with the last form. The problem is that, inside a constructor, the object is not yet fully constructed. An object is only fully constructed after its constructor completely returns, and not before. But when passed as a parameter to a method of some other object, the thisreference should always refer to a fully-formed object.