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

如果可以,如何使用它,用于什么目的?


当前回答

当然,抽象类可以有构造函数。通常使用类构造函数初始化字段。因此,抽象类构造函数用于初始化抽象类的字段。如果您想在子类实例化之前初始化抽象类的某些字段,则需要为抽象类提供构造函数。抽象类构造函数还可以用于执行与每个子类相关的代码。这可以防止代码复制。

我们不能创建抽象类的实例,但是我们可以创建从抽象类派生的类的实例。因此,当创建派生类的实例时,父抽象类构造函数将被自动调用。

参考:这篇文章

其他回答

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

是的,抽象类可以有构造函数。考虑一下:

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

父类Product是抽象的,并且有一个构造函数。具体类TimesTwo有一个只硬编码值2的构造函数。具体类TimesWhat有一个构造函数,允许调用者指定值。

抽象构造函数将经常用于强制类约束或不变量,例如设置类所需的最小字段。

注意:因为在父类中没有默认(或无参数)构造函数 抽象类,在子类中使用的构造函数必须显式调用 父构造函数。

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

虽然有很多好的答案,但我还是想提出我的意见。

构造函数不构建对象。它用于初始化对象。

是的,抽象类总是有一个构造函数。如果您没有定义自己的构造函数,编译器将为Abstract类提供一个默认构造函数。 以上对所有类都适用——嵌套的、抽象的、匿名的等等。

抽象类(与接口不同)可以具有需要初始化的非最终非静态字段。您可以在抽象类中编写自己的构造函数来实现这一点。但是,在这种情况下,不会有任何默认构造函数。

public abstract class Abs{
    int i;
    int j;
    public Abs(int i,int j){
        this.i = i;
        this.j = j;
        System.out.println(i+" "+j);
    }
}

在扩展上述抽象类时要小心,必须从每个构造函数显式地调用super。任何构造函数的第一行都调用super()。如果您没有显式地调用super(), Java将为您完成。 以下代码将无法编译:

public class Imp extends Abs{

public Imp(int i, int j,int k, int l){
    System.out.println("2 arg");
}
}

你必须像下面的例子那样使用它:

public class Imp extends Abs{

public Imp(int i, int j,int k, int l){
    super(i,j);
    System.out.println("2 arg");
}
}

是的,抽象类可以有构造函数!

下面是一个在抽象类中使用构造函数的例子:

abstract class Figure { 

    double dim1;        
    double dim2; 

    Figure(double a, double b) {         
        dim1 = a;         
        dim2 = b;         
    }

    // area is now an abstract method 

   abstract double area(); 

}


class Rectangle extends Figure { 
    Rectangle(double a, double b) { 
        super(a, b); 
    } 
    // override area for rectangle 
    double area() { 
        System.out.println("Inside Area for Rectangle."); 
        return dim1 * dim2; 
    } 
}

class Triangle extends Figure { 
    Triangle(double a, double b) { 
        super(a, b); 
    } 
    // override area for right triangle 
    double area() { 
        System.out.println("Inside Area for Triangle."); 
        return dim1 * dim2 / 2; 
    } 
}

class AbstractAreas { 
    public static void main(String args[]) { 
        // Figure f = new Figure(10, 10); // illegal now 
        Rectangle r = new Rectangle(9, 5); 
        Triangle t = new Triangle(10, 8); 
        Figure figref; // this is OK, no object is created 
        figref = r; 
        System.out.println("Area is " + figref.area()); 
        figref = t; 
        System.out.println("Area is " + figref.area()); 
    } 
}

我想你已经得到答案了。