构造函数中是否有必要添加 super() ?
如果我不将其放入子类的构造函数中,这不是由编译器自动放入的吗?
也就是说,我根本不需要关心它?他们在一些文章中提出了这一点。
如果我有一个带参数的构造函数,这将是构造函数,还是采用不带参数列表的构造函数?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先是一些术语:
所以所有类都至少有一个构造函数。
子类构造函数可能首先指定在执行子类构造函数中的代码之前调用超类中的哪个构造函数。
如果子类构造函数没有指定要调用哪个超类构造函数,则编译器将自动调用超类中可访问的无参数构造函数。
如果超类没有无参数构造函数或不可访问那么不指定要调用的超类构造函数(在子类构造函数中)会出现编译器错误,因此必须指定它。
例如:
这很好,因为如果您没有显式添加构造函数,Java 会为您添加一个公共默认构造函数。
也还好。
上面是一个编译错误,因为 Base 没有默认构造函数。
这也是一个错误,因为 Base 的无参数构造函数是私有的。
Firstly some terminology:
So all classes have at least one constructor.
Subclasses constructors may specify 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 must be specified.
For example:
This is fine because if you add no constructor explicitly Java puts in a public default constructor for you.
Also fine.
The above is a compilation error as Base has no default constructor.
This is also an error because Base's no-args constructor is private.
如果超类构造函数没有参数,Java 会自动为您调用它。如果它有参数,你会得到一个错误。
src: http://java.sun.com/docs/books /tutorial/java/IandI/super.html
If the super class constructor has no arguments Java automatically calls it for you. If it has arguments you'll get an error.
src: http://java.sun.com/docs/books/tutorial/java/IandI/super.html
调用无参数超级构造函数只是浪费屏幕空间和程序员时间。无论您是否编写,编译器都会生成完全相同的代码。
更新(2018 年 12 月):
编写显式
super()
有助于在 IDE 中导航源代码。截至 2018 年 12 月,Eclipse 和 IntelliJ 均未提供任何从构造函数轻松导航的方法派生类到基类的构造函数。
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.
Update (December 2018):
Writing an explicit
super()
helps navigating the source code in the IDE.As of December 2018, neither Eclipse nor IntelliJ provide any means of comfortably navigating from the constructor of the derived class to the constructor of the base class.
即使您不调用默认父构造函数,也会从子默认构造函数调用它。
主要
A
B
印刷品
Default parent constructor is called from child default constructor even if you do not call it.
Main
A
B
Prints
如果没有显式调用 super([arguments]),任何类构造函数总是调用“super()”,只有我们在编程时记住对超类构造函数的访问......
当我们不扩展任何特定类时,自动扩展 java.lang.Object 类
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 关键字..
它只是调用超类构造函数来创建对象。
情况1:当存在超类时,
super关键字会隐式添加到派生类中。
所以上面的代码与
情况2:(当有超类时)相同,但构造函数是重载
情况3:(当没有您定义的超类时)。
众所周知,每个类都是 Object 类的子类。或者我们可以说基类是所有类的超类。
所以现在添加或不添加 super 没有任何意义.. jvm 将默认添加它。
lets remember super keyword..
its nothing but calling the superclass constructor for creating the object.
case 1: when there is super class
here super keyword is implicitly added in derived class.
so the above code is same as this
case 2: when there is super class but constructor is overload
case 3: when there is no super class defined by you.
As we know every class is subclass of Object class.. or we can say base class is super class of all the classes.
so now add super or not does not make any sense.. by jvm it will be added by default.
如果超类可以有一个无参数构造函数。最好有一个无参数构造函数,否则你必须传递带参数的超级构造函数。
如果超类没有无参数构造函数或者不可访问,则不指定要调用的超类构造函数(在子类构造函数中)会出现编译器错误,因此必须指定它
}
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
}