为什么Java中的抽象类有构造函数?
为什么 Java 中的抽象
类有一个构造函数
?
它正在构造什么,因为我们无法实例化抽象类?
有什么想法吗?
Why does an abstract
class in Java have a constructor
?
What is it constructing, as we can't instantiate an abstract
class?
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Java 中的构造函数实际上并不“构建”对象,它用于初始化字段。
想象一下,你的抽象类有字段 x 和 y,并且你总是希望它们以某种方式初始化,无论最终创建什么实际的具体子类。因此,您创建一个构造函数并初始化这些字段。
现在,如果抽象类有两个不同的子类,当您实例化它们时,将调用它们的构造函数,然后调用父构造函数并初始化字段。
如果您不执行任何操作,则将调用父级的默认构造函数。但是,您可以使用 super 关键字来调用父类上的特定构造函数。
A constructor in Java doesn't actually "build" the object, it is used to initialize fields.
Imagine that your abstract class has fields x and y, and that you always want them to be initialized in a certain way, no matter what actual concrete subclass is eventually created. So you create a constructor and initialize these fields.
Now, if you have two different subclasses of your abstract class, when you instantiate them their constructors will be called, and then the parent constructor will be called and the fields will be initialized.
If you don't do anything, the default constructor of the parent will be called. However, you can use the super keyword to invoke specific constructor on the parent class.
这样做的两个原因:
1) 抽象类具有构造函数,并且在实例化具体子类时始终会调用这些构造函数。我们知道,当我们要实例化一个类时,我们总是使用该类的构造函数。现在,每个构造函数都会通过隐式调用
super()
来调用其超类的构造函数。2)我们知道构造函数也用于初始化类的字段。我们还知道抽象类可能包含字段,有时需要使用构造函数以某种方式初始化。
Two reasons for this:
1) Abstract classes have
constructors
and those constructors are always invoked when a concrete subclass is instantiated. We know that when we are going to instantiate a class, we always use constructor of that class. Now every constructor invokes the constructor of its super class with an implicit call tosuper()
.2) We know constructor are also used to initialize fields of a class. We also know that abstract classes may contain fields and sometimes they need to be initialized somehow by using constructor.
包括抽象类在内的所有类都可以有构造函数。抽象类的构造函数将在实例化其具体子类时被调用
All the classes including the abstract classes can have constructors.Abstract class constructors will be called when its concrete subclass will be instantiated
因为另一个类可以扩展它,并且子类需要调用超类构造函数。
Because another class could extend it, and the child class needs to invoke a superclass constructor.
我想这个问题的根源是人们相信对构造函数的调用会创建对象。事实并非如此。 Java 没有任何地方声称构造函数调用会创建对象。它只是做我们希望构造函数做的事情,比如初始化一些字段……仅此而已。因此,调用抽象类的构造函数并不意味着创建了它的对象。
I guess root of this question is that people believe that a call to a constructor creates the object. That is not the case. Java nowhere claims that a constructor call creates an object. It just does what we want constructor to do, like initialising some fields..that's all. So an abstract class's constructor being called doesn't mean that its object is created.
因为抽象类有状态(字段),有时它们需要以某种方式初始化。
Because abstract classes have state (fields) and somethimes they need to be initialized somehow.
在实现方面,您经常会在子类构造函数中看到 super() 语句,如下所示:
Implementation wise you will often see inside super() statement in subclasses constructors, something like: