为什么Java中的抽象类有构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2170500/
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
Why do abstract classes in Java have constructors?
提问by gameover
Why does an abstract
class in Java have a constructor
?
为什么abstract
Java中的类有一个constructor
?
What is it constructing, as we can't instantiate an abstract
class?
它在构造什么,因为我们不能实例化一个abstract
类?
Any thoughts?
有什么想法吗?
采纳答案by Uri
A constructor in Java doesn't actually "build" the object, it is used to initialize fields.
Java 中的构造函数实际上并不“构建”对象,它用于初始化字段。
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.
想象一下,您的抽象类具有字段 x 和 y,并且您总是希望它们以某种方式进行初始化,无论最终创建的是什么实际的具体子类。所以你创建一个构造函数并初始化这些字段。
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.
如果你什么都不做,就会调用父级的默认构造函数。但是,您可以使用 super 关键字来调用父类上的特定构造函数。
回答by Richard JP Le Guen
Because another class could extend it, and the child class needs to invoke a superclass constructor.
因为另一个类可以扩展它,而子类需要调用超类构造函数。
回答by Upul Bandara
All the classes including the abstract classes can have constructors.Abstract class constructors will be called when its concrete subclass will be instantiated
包括抽象类在内的所有类都可以有构造函数。抽象类的构造函数将在其具体子类被实例化时被调用
回答by manuel aldana
Implementation wise you will often see inside super() statement in subclasses constructors, something like:
在实现方面,您经常会在子类构造函数中的 super() 语句中看到,例如:
public class A extends AbstractB{
public A(...){
super(String constructorArgForB, ...);
...
}
}
回答by Andriy Sholokh
Because abstract classes have state (fields) and somethimes they need to be initialized somehow.
因为抽象类有状态(字段)和一些它们需要以某种方式初始化。
回答by Debmalya
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 to super()
.
1) 抽象类具有,constructors
并且在实例化具体子类时始终会调用这些构造函数。我们知道,当我们要实例化一个类时,我们总是使用该类的构造函数。现在,每个构造函数都通过对 的隐式调用来super()
调用其超类的构造函数。
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.
2)我们知道构造函数也用于初始化类的字段。我们也知道抽象类可能包含字段,有时需要使用构造函数以某种方式初始化它们。
回答by Mandroid
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.
我想这个问题的根源是人们相信对构造函数的调用会创建对象。事实并非如此。Java 无处声称构造函数调用会创建对象。它只是做我们想让构造函数做的事情,比如初始化一些字段......就是这样。所以抽象类的构造函数被调用并不意味着它的对象被创建。