Java 是否没有必要将 super() 放在构造函数中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2054022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:03:01  来源:igfitidea点击:

Is it unnecessary to put super() in constructor?

javaconstructorsuper

提问by ajsie

Isn't this one automatically put by the compiler if I don't put it in a subclass's constructor?

如果我不把它放在子类的构造函数中,这不是由编译器自动放置的吗?

That means I don't even need to care about it? In some articles they put it out.

这意味着我什至不需要关心它吗?在一些文章中,他们把它放出来了。

And if I've got one constructor with arguments, will this be the constructor, or does it take a constructor without argument list?

如果我有一个带参数的构造函数,这将是构造函数,还是需要一个没有参数列表的构造函数?

采纳答案by cletus

Firstly some terminology:

首先是一些术语:

  • No-args constructor:a constructor with no parameters;
  • Accessible no-args constructor:a no-args constructor in the superclass visible to the subclass. That means it is either public or protected or, if both classes are in the same package, package access; and
  • Default constructor:the public no-args constructor added by the compiler when there is no explicit constructor in the class.
  • 无参数构造函数:无参数构造函数;
  • 可访问的无参数构造函数:子类可见的超类中的无参数构造函数。这意味着它要么是公共的,要么是受保护的,或者,如果两个类都在同一个包中,则包访问;和
  • 默认构造函数:当类中没有显式构造函数时,编译器添加的公共无参数构造函数。

So all classes have at least one constructor.

所以所有的类都至少有一个构造函数。

Subclasses constructors mayspecify as the first thing they do which constructor in the superclass to invoke before executing the code in the subclass's constructor.

子类构造函数可以指定在执行子类构造函数中的代码之前调用超类中的哪个构造函数作为他们做的第一件事。

If the subclass constructor does not specify which superclass constructor to invoke then the compiler will automatically call the accessible no-args constructor in the superclass.

如果子类构造函数没有指定要调用哪个超类构造函数,那么编译器将自动调用超类中可访问的无参数构造函数。

If the superclass has no no-arg constructor or it isn't accessible then not specifying the superclass constructor to be called (in the subclass constructor) is a compiler error so it mustbe specified.

如果超类没有无参数构造函数或者它不可访问,那么不指定要调用的超类构造函数(在子类构造函数中)是编译器错误,因此必须指定。

For example:

例如:

public class Base { }
public class Derived extends Base { }

This is fine because if you add no constructor explicitly Java puts in a public default constructor for you.

这很好,因为如果您没有显式添加构造函数,Java 会为您放入一个公共默认构造函数。

public class Base { }
public class Derived extends Base { public Derived(int i) { } }

Also fine.

也不错。

public class Base { public Base(String s) { } }
public class Derived extends Base { }

The above is a compilation error as Base has no default constructor.

以上是编译错误,因为 Base 没有默认构造函数。

public class Base { private Base() { } }
public class Derived extends Base { }

This is also an error because Base's no-args constructor is private.

这也是一个错误,因为 Base 的无参数构造函数是私有的。

回答by lemon

If the super class constructor has no arguments Java automatically calls it for you. If it has arguments you'll get an error.

如果超类构造函数没有参数,Java 会自动为您调用它。如果它有参数,你会得到一个错误。

src: http://java.sun.com/docs/books/tutorial/java/IandI/super.html

源代码:http: //java.sun.com/docs/books/tutorial/java/IandI/super.html

回答by abhishek ringsia

abstract class Book
 {
String title;
String author;
Book(String t,String a){
    title=t;
    author=a;
}
abstract void display();

}    

If super class can have a No-args constructor .It is good to have a no argument constructor otherwise you have to pass super constructor with parameter .

如果超类可以有一个无参数构造函数。最好有一个无参数构造函数,否则你必须传递带有参数的超级构造函数。

If the superclass has no no-arg constructor or it isn't accessible then not specifying the superclass constructor to be called (in the subclass constructor) is a compiler error so it must be specified

如果超类没有无参数构造函数或它不可访问,则未指定要调用的超类构造函数(在子类构造函数中)是编译器错误,因此必须指定

class MyBook extends Book{   
int price ;
public  MyBook(String t,String a,int price){
     super(t,a);
    this.price=price;
}

public void display(){
    System.out.println("Title: "+title);
 System.out.println("Author: "+author); 
System.out.println("Price: "+price);

}

}

}

回答by Roland Illig

Calling the no-arguments super constructor is just a waste of screen space and programmer time. The compiler generates exactly the same code, whether you write it or not.

调用无参数的超级构造函数只是浪费屏幕空间和程序员时间。无论您是否编写,编译器都会生成完全相同的代码。

class Explicit() {
    Explicit() {
        super();
    }
}

class Implicit {
    Implicit() {
    }
}


Update (December 2018):

更新(2018 年 12 月):

Writing an explicit super()helps navigating the source code in the IDE.

编写显式super()有助于在 IDE 中导航源代码。

As of December 2018, neither Eclipse nor IntelliJprovide any means of comfortably navigating from the constructor of the derived class to the constructor of the base class.

截至 2018 年 12 月,Eclipse 和IntelliJ都没有提供任何从派生类的构造函数轻松导航到基类的构造函数的方法。

回答by Dharmendrasinh Chudasama

Any class constructor always calls "super()" if there is not explicitly called super([arguments]), only we keep in mind access of super class constructor while programming... when we not extends any specific class automatic extends java.lang.Object class

如果没有显式调用 super([arguments]),任何类构造函数总是调用“super()”,只有我们在编程时记住访问超类构造函数......当我们不扩展任何特定类时,会自动扩展 java.lang .Object 类

回答by Yan Khonski

Default parent constructor is called from child default constructor even if you do not call it.

即使您不调用它,也会从子默认构造函数调用默认父构造函数。

Main

主要的

public class Main {

    public static void main(String[] args) {
        A a = new B();
    }
}

A

一种

public class A {

    public A() {
        System.out.println("A");
    }
}

B

public class B extends A {

    public B() {
        System.out.println("B");
    }
}

Prints

印刷

A
B