了解Java构造函数链接中this()和(super)的用法

Constructor chaining in Java is simply the act of one constructor calling another constructor via inheritance. This happens implicitly when a subclass is constructed: its first task is to call its parent's constructor method. But programmers can also call another constructor explicitly using the keywords this() or super(). The this() keyword calls another overloaded constructor in the same class; the super() keyword calls a non-default constructor in a superclass.

Java中的构造函数链接只是一个构造函数通过继承调用另一个构造函数的行为。 这在构造子类时隐式发生:它的第一个任务是调用其父级的构造方法。 但是程序员也可以使用关键字this()super()显式调用另一个构造函数。 关键字this()调用同一类中的另一个重载的构造函数super()关键字在超类中调用非默认构造函数。

隐式构造器链接 ( Implicit Constructor Chaining )

Constructor chaining occurs through the use of inheritance. A subclass constructor method's first task is to call its superclass' constructor method. This ensures that the creation of the subclass object starts with the initialization of the classes above it in the inheritance chain.

构造函数链接通过使用继承发生。 子类构造函数方法的第一个任务是调用其超类的构造函数方法。 这样可以确保子类对象的创建始于继承链中其上方的类的初始化。

There could be any number of classes in an inheritance chain. Every constructor method calls up the chain until the class at the top has been reached and initialized. Then each subsequent class below is initialized as the chain winds back down to the original subclass. This process is called constructor chaining.

继承链中可以有任意多个类。 每个构造函数方法都会调用该链,直到到达顶部的类并对其进行初始化为止。 然后,在链回绕到原始子类时,将初始化下面的每个后续类。 此过程称为构造函数链接。

Note that:

注意:

  • This implicit call to the superclass is the same as if the subclass had included the super() keyword, i.e. super() is implicit here.

    对超类的隐式调用与子类包含super()关键字的隐式调用相同,即, super()在此是隐式的。

  • If a no-args constructor is not included in the class, Java creates one behind the scenes and invokes it. This means that if your only constructor takes an argument, you must explicitly use a this() or super() keyword to invoke it (see below).

    如果该类中不包含no-args构造函数,则Java在后台创建一个并调用它。 这意味着,如果您唯一的构造函数接受一个参数,则必须显式使用this()super()关键字来调用它(请参见下文)。

Consider this superclass Animal extended by Mammal:

考虑一下由哺乳动物扩展的超类动物:


class Animal {
// construct

 System.out.println("We're in class Animal's constructor."


class Mammal extends Animal {
//construct

 System.out.println("We're in class Mammal 's constructor."

Now, let's instantiate the class Mammal:

现在,让我们实例化哺乳动物类:


public class Chain


 /**
* @param args
*/
public static void main(String[] args) {
Mammal m = new Mammal

When the above program runs, Java implicitly triggers a call to the superclass Animal constructor, then to the class' constructor. The output, therefore, will be:

当上述程序运行时,Java隐式触发对超类Animal构造函数的调用,然后对该类的构造函数的调用。 因此,输出为:


We're in class Animal's constructor
We're in class Mammal&

使用this()或super()的显式构造方法链接 ( Explicit Constructor Chaining using this() or super() )

Explicit use of the this() or super() keywords allows you to call a non-default constructor.

显式使用this()super()关键字使您可以调用非默认构造函数。

  • To call a non-args default constructor or an overloaded constructor from within the same class, use the this() keyword. 

    要从同一类中调用非参数默认构造函数或重载构造函数,请使用this()关键字。

  • To call a non-default superclass constructor from a subclass, use the super() keyword. For instance, if the superclass has multiple constructors, a subclass may always want to call a specific constructor, rather than the default.

    要从子类中调用非默认超类构造函数,请使用super()关键字。 例如,如果超类具有多个构造函数,则子类可能总是要调用特定的构造函数,而不是默认构造函数。

Note that the call to another constructor must be the first statement in the constructor or Java will throw a compilation error.

请注意,对另一个构造函数的调用必须是该构造函数中的第一条语句,否则Java将引发编译错误。

Consider the code below in which a new subclass, Carnivore, inherits from Mammal class which inherits from the Animal class, and each class now has a constructor that takes an argument.

考虑下面的代码,其中一个新的子类Carnivore从Mammal类继承而来,而Mammal类继承自Animal类,并且每个类现在都有一个带参数的构造函数。

Here's the superclass Animal: 

这是超类动物:


public class Animal
private String name;
public Animal(String name) // constructor with an argument
{
this.name = name;
System.out.println("I'm executed first.");
}
}
Note that the constructor now takes a
name of type String类型的 String as a parameter and that the body of the class calls 名称作为参数,并且类的主体在构造函数上调用 this() on the constructor. Without the explicit use of this() 。 如果不显式使用 this.name, Java would create a default, no-args constructor an this.name ,Java将创建一个默认的无参数构造函数,并且

Here's the subclass Mammal:

这是哺乳动物的子类:


public class Mammal extends Animal {
public Mammal(String name)
{
super(name);
System.out.println("I'm executed second"

Its constructor also takes an argument, and it uses super(name) to invoke a specific constructor in its superclass.

它的构造函数还带有一个参数,并且使用super(name)调用其超类中的特定构造函数。

Here's another subclass Carnivore. This inherits from Mammal: 

这是食肉动物的另一个子类。 这是从哺乳动物继承的:


public class Carnivore extends Mammal{
public Carnivore(String name)
{
super(name);
System.out.println("I'm executed last"

When run, these three code blocks would print:

运行时,这三个代码块将打印:


I'm executed first.
I'm executed second.
I'

To recap: When an instance of the Carnivore class is created, the first action of its constructor method is to call the Mammal constructor method. Likewise, the first action of the Mammal constructor method is to call the Animal constructor method. A chain of constructor method calls ensure that the instance of the Carnivore object has properly initialized all the classes in its inheritance chain.

回顾一下 :创建Carnivore类的实例时,其构造方法的第一步是调用Mammal构造方法。 同样,哺乳动物构造函数的第一个动作是调用动物构造函数。 一系列构造函数方法调用可确保Carnivore对象的实例已正确初始化其继承链中的所有类。

翻译自: https://www.thoughtco.com/constructor-chaining-2034057

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值