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

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


当前回答

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

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

是的,抽象类总是有一个构造函数。如果您没有定义自己的构造函数,编译器将为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");
}
}

其他回答

在具体类中,具体类型Fnord的构造函数声明有效地公开了两件事:

一种代码可以请求创建Fnord实例的方法 一种方法,通过这种方法,正在构造中的派生自Fnord的类型实例可以请求初始化所有基类特性。

虽然应该有一种方法可以分别控制这两种能力,但对于每一种具体类型,都有一个定义可以同时实现这两种能力。尽管第一个能力对抽象类没有意义,但第二个能力对抽象类和其他类一样有意义,因此它的声明是必要和有用的。

如果你处于以下情况之一,你可以在抽象类中定义构造函数:

你想要表演一些 属性的字段的初始化 抽象类)之前 一个子类的实例化 发生 属性中定义了最终字段 抽象类,但你没有 在声明中初始化它们 本身;在这种情况下,你必须 初始化这些函数的构造函数 字段

注意:

您可以定义多个 构造函数(使用不同的 参数) 你可以(应该?)定义你所有的 构造函数受保护(使它们 反正公开是没有意义的) 你的子类构造函数可以 调用摘要的一个构造函数 类;它甚至可能不得不调用它 (如果没有无参数构造函数 在抽象类中)

在任何情况下,不要忘记如果你没有定义构造函数,那么编译器将自动为你生成一个构造函数(这个构造函数是公共的,没有参数,什么也不做)。

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

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

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

我想你已经得到答案了。

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

它不仅可以,而且总是这样。如果你没有指定一个,那么它就有一个默认的无参数构造函数,就像任何其他类一样。事实上,所有的类,包括嵌套类和匿名类,如果没有指定一个默认构造函数,就会得到一个默认构造函数(在匿名类的情况下,不可能指定一个,所以您总是会得到默认构造函数)。

具有构造函数的抽象类的一个很好的例子是Calendar类。您可以通过调用Calendar. getinstance()来获得Calendar对象,但它也有受保护的构造函数。它的构造函数被保护的原因是,只有它的子类才能调用它们(或者同一个包中的类,但因为它是抽象的,所以不适用)。GregorianCalendar是一个扩展Calendar的类的例子。