简体   繁体   English

抽象类的构造函数

[英]Constructor in Abstract class

I am making a graphing library in Java (I know there are plenty out there already). 我正在用Java创建一个图形库(我知道那里已经有很多)。

I have an abstract class "Graph" which has variables for width and height and methods to draw the scale and an abstract method to draw the graph. 我有一个抽象类“ Graph”,它具有用于宽度和高度的变量以及用于绘制比例的方法和用于绘制图形的抽象方法。 I have multiple classes which extend the Graph class such as LineGraph, BarGraph etc which have a method to draw the graph. 我有多个类来扩展Graph类,例如LineGraph,BarGraph等,它们具有绘制图的方法。

As the width and height of a graph need to be set when a new instance of a graph is created I would like to do it in the constructor but when I tried Eclipse still wanted me to make a separate constructor in each class. 由于在创建图的新实例时需要设置图的宽度和高度,因此我想在构造函数中进行设置,但是当我尝试使用Eclipse时,仍然希望我在每个类中创建一个单独的构造函数。 Is it possible to have an abstract class with a constructor and how would you do it? 可能有一个带有构造函数的抽象类,您将如何做?

You can have constructor in abstract class which takes in width and height. 您可以在抽象类中使用构造函数,该构造函数采用宽度和高度。 But still you need to have constructor in each of the derived classes which take width and height, but delegates the processing to super class. 但是,仍然需要在每个派生类中都有构造函数,这些构造函数采用宽度和高度,但是将处理委托给超类。

You might have to do something like given below, 您可能需要执行以下操作,

public abstract class Graph {
   public Graph(int width, int height) {
       // do actual things with width and height
   }
}

public class LineGraph extends Graph{
   public LineGraph(int width, int height) {
      super(width, height);
      // additional inits.
   }
}

public class BarGraph extends Graph{
   public BarGraph(int width, int height) {
      super(width, height);
      // additional inits.
   }
}

Its not eclipse. 它不蚀。 It's the Java Compiler which is forcing you do it. 是Java编译器迫使您执行此操作。 If you have defined the constructor in Abstract Class. 如果您在Abstract Class中定义了构造函数。 One must call that constructor from the derived class. 必须从派生类中调用该构造函数。

The concept of having a constructor in an abstract helps a programmer to assign a property to all inherited subclass. 在抽象中包含构造函数的概念有助于程序员将属性分配给所有继承的子类。

usually people use abstract constructor to initialize some property that can use in their children class. 通常,人们使用抽象构造函数初始化一些可以在其子类中使用的属性。

now if in your scenario you want to do so you can use this strategy . 现在,如果您想这样做,则可以使用此策略。

You can refer to this post to understand the concept better -> link 您可以参考这篇文章以更好地理解概念-> 链接

If you use inheritance, then you can use the base class constructor to assign values of height and width. 如果使用继承,则可以使用基类构造函数来分配height和width的值。 But in this case you need to either have default constructor in base class along with parametersed constructior or you have to call the parameterised constructor of base class from derived class.. 但是在这种情况下,您需要在基类中具有默认构造函数以及带参数的构造函数,或者必须从派生类调用基类的参数化构造函数。

For every subclass you need to create a constructor with the same signature (or not) of the super, so if you don't want any behavior in the constructor of the subclasses just add super(); 对于每个子类,您需要创建一个具有与父类相同(或没有)签名的构造函数,因此,如果您不希望子类的构造函数中发生任何行为,只需添加super();即可。

Example: 例:

abstract class Graph {
  int width;
  int height;

  public Graph(int width, int height) {
    this.width = width;
    this.height = height;
  }
}

class LinearGraph extends Graph {
  public LinearGraph(int width, int height) {
    super(width, height);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM