简体   繁体   English

为什么我们在构造函数中调用super()

[英]Why do we call super() in constructor

We have a constructor like this: 我们有一个像这样的构造函数:

public statusArea()     
{           
   super();         
   add(pageIdentifier);     
}

Why do we call super in it? 为什么我们称之为super呢?

From Oracle docs Oracle文档

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. 对象确实具有这样的构造函数,因此如果对象是唯一的超类,就没有问题。

super() invokes the parameterless constructor of the super class. super()调用超类的无参数构造函数。

In your code it makes no difference, since the compiler will add super() implicitly if you don't. 在您的代码中没有什么区别,因为如果不这样做,编译器将隐式添加super() In general, you add an explicit call to the super class's constructor if you wish to call a specific constructor of the super class. 通常,如果希望调用父类的特定构造函数,则可以向父类的构造函数添加显式调用。

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

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

super(); // the superclass no-arg constructor is called

or: 要么:

super(params);//the superclass constructor with a matching params is called

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. 对象确实具有这样的构造函数,因此如果对象是唯一的超类,就没有问题。

The purpose of the super keyword in Java is to allow you to access the instance fields, instance methods or the constructor of a super class from which a class extends. Java中super关键字的目的是允许您访问实例字段,实例方法或扩展类的超类的构造函数。

Since every class in Java extends from Object implicitly, a call to super() in a constructor for a class that does not extend from any class means a call to the no-arg constructor in the Object class. 由于Java中的每个类都隐式地从Object extend ,因此对不从任何类extend的类的构造函数中的super()调用意味着对Object类中的no-arg构造函数的调用。

Even if you don't put the super() call explicitly in a constructor, the compiler will add one. 即使您没有将super()调用明确地放在构造函数中,编译器也会添加一个。

Here you use super() to give the parent class (ie. the class which this one extends) a chance to initialize any fields or variables, so that your sublcass can work correctly. 在这里,您可以使用super()为父类(即该类扩展的类)提供初始化任何字段或变量的机会,以便您的子类可以正常工作。 For example, the superclass may have some fields or variables which you do not use in your subclass, but which are needed for the class to work correctly. 例如,超类可能包含一些您未在子类中使用的字段或变量,但是某些字段或变量是类正常工作所必需的。

In this case, super() invokes the parameterless constructor of the superclass, ie. 在这种情况下, super()调用超类的无参数构造函数,即。 the one which your class extends. 您的班级扩展的那个。

If you call super(...) with parameters, then the superclass constructor with the matching parameters is called. 如果使用参数调用super(...) ,则会调用具有匹配参数的超类构造函数。

As others have said, if you don't call super() yourself, the compiler will call it. 正如其他人所说,如果您自己不调用super() ,则编译器将调用它。

You only need to specify the constructor to call if: 仅在以下情况下需要指定要调用的构造函数:

You want to call a superclass constructor which has parameters 您要调用具有参数的超类构造函数

You want to chain to another constructor in the same class instead of the superclass constructor 您想链接到同一类中的另一个构造函数,而不是超类构造函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM