constructor、this()和super()

陈 欣发布

在Java中,constructor是一种特殊的method,可以接受零至多个参数但没有任何返回类型(或者从某种意义上,可以理解为constructor返回该类的一个object)。

constructor一般用来执行object相关的一系列初始化操作,只能在new关键字后被调用一次。比如我们用下面这个constructor给特定的Bicycle object的各个field赋值。

public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
}

当然,参数的名字也可以和field名字一样。但在这时函数的参数具有较高的优先级,为了区分起见需要用到this来调用field。

public Bicycle(int cadence, int speed, int gear) {
    this.gear = gear;
    this.cadence = cadence;
    this.speed = speed;
}

一个class不写任何constructor是没有问题的。这个class仍然可以用调用默认无参数constructor的方式来实例化SomeClass myObject=new SomeClass()。但是如果已经定义了一个有参数的constructor,比如public SomeClass(int a, int b),则必须也定义默认无参数constructor public SomeClass(),否则Java编译器会在上面的语句报错,认为可能没有调用正确的constructor。定义后直接调用有参数的constructor到也没有问题,比如SomeClass myClass=new SomeClass(3, 5)

因为Java语言支持多态(polymorphism)的特性,即允许存在同名但接受不同参数类型组合的函数,由编译器根据调用时给的参数来具体决定使用哪个函数,因而一个class可以有多个不同参的constructor。

public SomeClass(int a, int b) {
//Some constructor stuff
}

public SomeClass(String c) {
//Some more constructor stuff
}

使用this()可以在一个constructor里面调用class里的另外一个不同参的constructor。this()不允许递归,还必须是constructor的第一个语句。恰当的使用this()可以方便constructor的编写,尽最大可能的利用现有的语句避免重复累赘。

public SomeClass(int a, int b) {
//Some constructor stuff
}

public SomeClass(String c) {
this(1, 3); //call SomeClass(int a, int b)
//Some more constructor stuff
}

对于继承父类的子类,Java会默认最先调用远祖的constructor,再一级一级下来。例如这样的一个继承。

public class GrandParent {
    public GrandParent(){
        System.out.println("GrandParent constructor called!");
    }
}
public class Parent extends GrandParent{
    public Parent(){
        System.out.println("Parent constructor called!");
    }
}
public class SomeClass extends Parent {
    public SomeClass(){
        System.out.println("Constructor called!");
    }
}

在执行SomeClass myObject=new SomeClass()时会输出

GrandParent constructor called!
Parent constructor called!
Constructor called!

可以通过使用不同的参数组合来用super()调用parent class的特定constructor。super()也必须是constructor的第一个语句,因此它和this()并不能同时使用。

当然,之前提到的默认constructor问题这边也同样存在。如果其中的一个parent定义了有参数的constructor而未定义无参的默认constructor,它的child class就必须在第一句使用super()带上特定的参数显式调用已定义的constructor。

AP计算机科学不考察this(),但super()包括在考试范围之内。


陈 欣

AADPS创始人

0 条评论

发表回复