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

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


当前回答

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

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

注意:

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

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

其他回答

抽象类可以有构造函数,但它不能被实例化。但抽象类中定义的构造函数可用于该抽象类的具体类的实例化。检查JLS组合:

如果试图使用类实例创建创建抽象类的实例,则会出现编译时错误 表达式。 抽象类的子类本身可能不是抽象的 对象的构造函数的执行 抽象类,因此,字段初始化式的执行 对于该类的实例变量。

是的,抽象类可以有构造函数。可以在抽象类中重载任意数量的构造函数。这些承包商可以用来初始化扩展抽象类的对象的初始状态。正如我们所知,我们不能创建抽象类的对象,因为对象是由“new”关键字创建的,而不是由构造函数创建的……它们只用于初始化子类对象的状态。

正如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 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有一个构造函数,允许调用者指定值。

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

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

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

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

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

我想你已经得到答案了。