Java 抽象类可以有构造函数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/260666/
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
Can an abstract class have a constructor?
提问by Szere Dyeri
Can an abstract class have a constructor?
抽象类可以有构造函数吗?
If so, how can it be used and for what purposes?
如果是这样,如何使用它以及用于什么目的?
采纳答案by Michael Rutherfurd
Yes, an abstract class can have a constructor. Consider this:
是的,抽象类可以有一个构造函数。考虑一下:
abstract class Product {
int multiplyBy;
public Product( int multiplyBy ) {
this.multiplyBy = multiplyBy;
}
public int mutiply(int val) {
return multiplyBy * val;
}
}
class TimesTwo extends Product {
public TimesTwo() {
super(2);
}
}
class TimesWhat extends Product {
public TimesWhat(int what) {
super(what);
}
}
The superclass Product
is abstract and has a constructor. The concrete class TimesTwo
has a constructor that just hardcodes the value 2. The concrete class TimesWhat
has a constructor that allows the caller to specify the value.
超类Product
是抽象的并且有一个构造函数。具体类TimesTwo
有一个只对值 2 进行硬编码的构造函数。具体类TimesWhat
有一个构造函数,允许调用者指定值。
Abstract constructors will frequently be used to enforce class constraints or invariants such as the minimum fields required to setup the class.
抽象构造函数将经常用于强制类约束或不变量,例如设置类所需的最少字段。
NOTE: As there is no default (or no-arg) constructor in the parent abstract class, the constructor used in subclass must explicitly call the parent constructor.
注意:由于父抽象类中没有默认(或无参数)构造函数,子类中使用的构造函数必须显式调用父构造函数。
回答by S.Lott
Consider this:
考虑一下:
abstract class Product {
int value;
public Product( int val ) {
value= val;
}
abstract public int multiply();
}
class TimesTwo extends Product {
public int mutiply() {
return value * 2;
}
}
The superclass is abstract and has a constructor.
超类是抽象的并且有一个构造函数。
回答by Lawrence Dol
Yes it can have a constructor and it is defined and behaves just like any other class's constructor. Except that abstract classes can't be directly instantiated, only extended, so the use is therefore always from a subclass's constructor.
是的,它可以有一个构造函数,它的定义和行为就像任何其他类的构造函数一样。除了抽象类不能直接实例化,只能扩展,因此使用总是来自子类的构造函数。
回答by jfpoilpret
You would define a constructor in an abstract class if you are in one of these situations:
如果您处于以下情况之一,您将在抽象类中定义构造函数:
- you want to perform some initialization (to fields of the abstract class) before the instantiation of a subclass actually takes place
- you have defined final fields in the abstract class but you did not initialize them in the declaration itself; in this case, you MUST have a constructor to initialize these fields
- 您想在子类的实例化实际发生之前执行一些初始化(对抽象类的字段)
- 您已经在抽象类中定义了 final 字段,但没有在声明本身中初始化它们;在这种情况下,您必须有一个构造函数来初始化这些字段
Note that:
注意:
- you may define more than one constructor (with different arguments)
- you can (should?) define all your constructors protected (making them public is pointless anyway)
- your subclass constructor(s) can call one constructor of the abstract class; it may even have tocall it (if there is no no-arg constructor in the abstract class)
- 您可以定义多个构造函数(具有不同的参数)
- 您可以(应该?)定义所有受保护的构造函数(无论如何,将它们公开是没有意义的)
- 您的子类构造函数可以调用抽象类的一个构造函数;它甚至可能必须调用它(如果抽象类中没有无参数构造函数)
In any case, don't forget that if you don't define a constructor, then the compiler will automatically generate one for you (this one is public, has no argument, and does nothing).
无论如何,不要忘记,如果你不定义构造函数,那么编译器会自动为你生成一个(这个是公共的,没有参数,什么也不做)。
回答by Aniket Thakur
Yes! Abstract classes can have constructors!
是的!抽象类可以有构造函数!
Yes, when we define a class to be an Abstract Class it cannot be instantiated but that does not mean an Abstract class cannot have a constructor. Each abstract class must have a concrete subclass which will implement the abstract methods of that abstract class.
是的,当我们将一个类定义为抽象类时,它不能被实例化,但这并不意味着抽象类不能有构造函数。每个抽象类都必须有一个具体的子类,该子类将实现该抽象类的抽象方法。
When we create an object of any subclass all the constructors in the corresponding inheritance tree are invoked in the top to bottom approach. The same case applies to abstract classes. Though we cannot create an object of an abstract class, when we create an object of a class which is concrete and subclass of the abstract class, the constructor of the abstract class is automatically invoked. Hence we can have a constructor in abstract classes.
当我们创建任何子类的对象时,相应继承树中的所有构造函数都以自上而下的方式调用。同样的情况适用于抽象类。虽然我们不能创建抽象类的对象,但是当我们创建抽象类的具体子类的类的对象时,会自动调用抽象类的构造函数。因此,我们可以在抽象类中有一个构造函数。
Note: A non-abstract class cannot have abstract methods but an abstract class can have a non-abstract method. Reason is similar to that of constructors, difference being instead of getting invoked automatically we can call super(). Also, there is nothing like an abstract constructor as it makes no sense at all.
注意:非抽象类不能有抽象方法,但抽象类可以有非抽象方法。Reason 类似于构造函数,不同之处在于我们可以调用 super() 而不是自动调用。此外,没有什么比抽象构造函数更像的了,因为它完全没有意义。
回答by jaideep
As described by javafuns here, this is an example:
正如这里的 javafuns 所描述的,这是一个例子:
public abstract class TestEngine
{
private String engineId;
private String engineName;
public TestEngine(String engineId , String engineName)
{
this.engineId = engineId;
this.engineName = engineName;
}
//public gettors and settors
public abstract void scheduleTest();
}
public class JavaTestEngine extends TestEngine
{
private String typeName;
public JavaTestEngine(String engineId , String engineName , String typeName)
{
super(engineId , engineName);
this.typeName = typeName;
}
public void scheduleTest()
{
//do Stuff
}
}
回答by Jacob
Yes it can, abstract classes constructors are generally used for super calls for initialization events common to all the subclasses
是的,它可以,抽象类构造函数通常用于对所有子类共有的初始化事件的超级调用
回答by MattC
Not only can it, it always does. If you do not specify one then it has a default no arg constructor, just like any other class. In fact, ALL classes, including nested and anonymous classes, will get a default constructor if one is not specified (in the case of anonymous classes it is impossible to specify one, so you will always get the default constructor).
不仅可以,而且总是可以。如果你没有指定一个,那么它有一个默认的无参数构造函数,就像任何其他类一样。事实上,所有类,包括嵌套类和匿名类,如果没有指定,都会得到一个默认构造函数(在匿名类的情况下,不可能指定一个,所以你总是会得到默认构造函数)。
A good example of an abstract class having a constructor is the Calendarclass. You get a Calendar object by calling Calendar.getInstance(), but it also has constructors which are protected. The reason its constructors are protected is so that only its subclasses can call them (or classes in the same package, but since it's abstract, that doesn't apply). GregorianCalendaris an example of a class that extends Calendar.
具有构造函数的抽象类的一个很好的例子是Calendar类。您可以通过调用 Calendar.getInstance() 获得一个 Calendar 对象,但它也有受保护的构造函数。它的构造函数受到保护的原因是只有它的子类可以调用它们(或同一包中的类,但由于它是抽象的,因此不适用)。GregorianCalendar是一个扩展 Calendar 类的示例。
回答by supercat
In a concrete class, declaration of a constructor for a concrete type Fnord effectively exposes two things:
在具体类中,具体类型 Fnord 的构造函数声明有效地暴露了两件事:
A means by which code can request the creation of an instance of Fnord
A means by which an instance of a type derived from Fnordwhich is under construction can request that all base-class features be initialized.
代码可以请求创建 Fnord 实例的一种方式
一种从 Fnord 派生的类型实例可以请求初始化所有基类功能的方法。
While there should perhaps be a means by which these two abilities could be controlled separately, for every concrete type one definition will enable both. Although the first ability is not meaningful for an abstract class, the second ability is just as meaningful for an abstract class as it would be for any other, and thus its declaration is just as necessary and useful.
虽然也许应该有一种方法可以分别控制这两种能力,但对于每种具体类型,一个定义将同时启用这两种能力。虽然第一个能力对抽象类没有意义,但第二个能力对抽象类和其他任何能力一样有意义,因此它的声明同样必要和有用。
回答by Sandeep
Yes..It is like any other class. It can have a constructor and it is called after creating object for the base class.
是的..就像任何其他课程一样。它可以有一个构造函数,并在为基类创建对象后调用。