抽象类可以有构造函数吗?

本文翻译自:Can an abstract class have a constructor?

Can an abstract class have a constructor? 抽象类可以有构造函数吗?

If so, how can it be used and for what purposes? 如果是这样,它如何使用以及用于何种目的?


#1楼

参考:https://stackoom.com/question/15oI/抽象类可以有构造函数吗


#2楼

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(). Reason与构造函数类似,不同之处在于我们可以调用super()而不是自动调用。 Also, there is nothing like an abstract constructor as it makes no sense at all. 此外,没有什么比抽象构造函数更好,因为它完全没有意义。


#3楼

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
   }
}

#4楼

是的,抽象类构造函数通常用于对所有子类共有的初始化事件的超级调用


#5楼

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. 如果你没有指定一个,那么它有一个默认的没有arg构造函数,就像任何其他类一样。 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 Calendar class. 具有构造函数的抽象类的一个很好的例子是Calendar类。 You get a Calendar object by calling Calendar.getInstance(), but it also has constructors which are protected. 通过调用Calendar.getInstance()获得Calendar对象,但它也有受保护的构造函数。 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). 它的构造函数受到保护的原因是只有它的子类可以调用它们(或者同一个包中的类,但因为它是抽象的,所以不适用)。 GregorianCalendar is an example of a class that extends Calendar. GregorianCalendar是扩展Calendar的类的示例。


#6楼

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 代码可以请求创建Fnord实例的方法

  • A means by which an instance of a type derived from Fnord which is under construction can request that all base-class features be initialized. 正在构造的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. 虽然第一种能力对于抽象类没有意义,但第二种能力对于抽象类来说和其他任何能力一样有意义,因此它的声明同样必要且有用。

发布了0 篇原创文章 · 获赞 52 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/CHCH998/article/details/105659099