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

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


当前回答

为了实现构造函数链接,抽象类将有一个构造函数。 编译器将Super()语句保存在子类构造函数中,该语句将调用父类构造函数。如果抽象类没有构造函数,那么就违反了java规则,我们就无法实现构造函数链接。

其他回答

一个抽象类可以有一个构造函数,但是你不能创建一个抽象类的对象,那么你如何使用那个构造函数呢?

问题是,当你在子类中继承抽象类时,你可以通过子类中的super(value)方法将值传递给它的(抽象的)构造函数,不,你不继承构造函数。

所以使用super,你可以在抽象类的构造函数中传递值,据我所知,它必须是你的方法或构造函数中的第一个语句。

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

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

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

类中构造函数的作用是初始化字段,而不是“构建对象”。当您尝试创建一个抽象SuperClass的新实例时,编译器会给您一个错误。然而,我们可以继承一个抽象类Employee,并通过设置其变量来使用其构造函数(参见下面的示例)

public abstract class Employee {
  private String EmpName;
  abstract double calcSalary();

  Employee(String name) {
    this.EmpName = name;// constructor of abstract class super class
  }
}

class Manager extends Employee{
 Manager(String name) {
    super(name);// setting the name in the constructor of sub class
 }
double calcSalary() {
    return 0;
 }
}

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

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

注意:

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

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

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

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

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

我想你已经得到答案了。