这个()和这个

JJ *_*Liu 4 java this

Java有this()吗?

如果是这样,this和之间的区别是this()什么?

Nis*_*ant 12

this是对当前实例的引用.this()调用默认构造函数.super()明确的调用默认超类的构造函数.


Ted*_*opp 8

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)