Documentation

The Java™ Tutorials
Hide TOC
Using the Keyword super使用关键字super
Trail: Learning the Java Language
Lesson: Interfaces and Inheritance
Section: Inheritance

Using the Keyword super使用关键字super

Accessing Superclass Members访问超类成员

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. 如果您的方法重写其超类的方法之一,则可以通过使用关键字super调用被重写的方法。You can also use super to refer to a hidden field (although hiding fields is discouraged). 您还可以使用super来引用隐藏字段(尽管不鼓励隐藏字段)。Consider this class, Superclass:考虑这个类,Superclass

public class Superclass {

    public void printMethod() {
        System.out.println("Printed in Superclass.");
    }
}

Here is a subclass, called Subclass, that overrides printMethod():下面是一个名为Subclass的子类,它覆盖printMethod()

public class Subclass extends Superclass {

    // overrides printMethod in Superclass
    public void printMethod() {
        super.printMethod();
        System.out.println("Printed in Subclass");
    }
    public static void main(String[] args) {
        Subclass s = new Subclass();
        s.printMethod();    
    }
}

Within Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass. Subclass中,简单名称printMethod()引用在子类中声明的名称,它覆盖了超类中的名称。So, to refer to printMethod() inherited from Superclass, Subclass must use a qualified name, using super as shown. 所以,要引用从超类继承的printMethod(),子类必须使用限定名,使用super,如图所示。Compiling and executing Subclass prints the following:编译和执行Subclass将打印以下内容:

Printed in Superclass.
Printed in Subclass

Subclass Constructors子类构造函数

The following example illustrates how to use the super keyword to invoke a superclass's constructor. 下面的示例说明了如何使用super关键字调用超类的构造函数。Recall from the Bicycle example that MountainBike is a subclass of Bicycle. 回想一下Bicycle的例子,MountainBikeBicycle的一个子类。Here is the MountainBike (subclass) constructor that calls the superclass constructor and then adds initialization code of its own:下面是MountainBike(子类)构造函数,它调用超类构造函数,然后添加自己的初始化代码:

public MountainBike(int startHeight, 
                    int startCadence,
                    int startSpeed,
                    int startGear) {
    super(startCadence, startSpeed, startGear);
    seatHeight = startHeight;
}

Invocation of a superclass constructor must be the first line in the subclass constructor.调用超类构造函数必须是子类构造函数的第一行。

The syntax for calling a superclass constructor is调用超类构造函数的语法是

super();
or:
super(parameter list);

With super(), the superclass no-argument constructor is called. 使用super(),将调用超类无参数构造函数。With super(parameter list), the superclass constructor with a matching parameter list is called.使用super(parameter list),将调用具有匹配参数列表的超类构造函数。


Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. 如果构造函数没有显式调用超类构造函数,Java编译器会自动插入对该超类的无参数构造函数的调用。If the super class does not have a no-argument constructor, you will get a compile-time error. 如果超类没有无参数构造函数,则会出现编译时错误。Object does have such a constructor, so if Object is the only superclass, there is no problem. Object确实有这样的构造函数,所以如果Object是唯一的超类,就没有问题了。

If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object. 如果子类构造函数显式或隐式调用其超类的构造函数,您可能会认为会调用整个构造函数链,一直返回到Object的构造函数。In fact, this is the case. 事实上,情况就是这样。It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.这称为构造函数链,当有一长串类下降时,您需要注意它。


Previous page: Hiding Fields
Next page: Object as a Superclass