this是对当前对象的引用.this()是对默认构造函数的调用; 它只在另一个构造函数中合法,并且只作为构造函数中的第一个语句.您还可以调用super()以调用超类的默认构造函数(同样,仅作为构造函数的第一个语句).事实上,如果代码中没有this()或super()(有或没有参数),编译器会自动插入.例如:
public class A {
A() {
super(); // call to default superclass constructor.
}
A(int arg) {
this(); // invokes default constructor
// do something special with arg
}
A(int arg, int arg2) {
this(arg); // invokes above constructor
// do something with arg2
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1291 次 |
| 最近记录: |