The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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
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
的例子,MountainBike
是Bicycle
的一个子类。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();
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)
,将调用具有匹配参数列表的超类构造函数。
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.这称为构造函数链,当有一长串类下降时,您需要注意它。