簡體   English   中英

“抽象類中沒有默認的構造函數”

[英]“no default constructor available in abstract class”

我只有一個實現相同抽象類的具體類中出現沒有默認構造函數可用的錯誤,我不確定為什么,任何幫助都將不勝感激。

`

public abstract class Employee implements Payable
    private  String firstName;
    private  String lastName;
    private  String socialSecurityNumber;
    private date birthdate;

          // constructor
          public Employee(String firstName, String lastName,
             String social, date dob )
          {
             this.firstName = firstName;
             this.lastName = lastName;
            this.socialSecurityNumber = social;
             //this.birthdate = getBirthdate();
              // Birthdate(year, month, day);
              birthdate = dob;
          }



public class pieceWorker extends Employee  // no default constructor available
{
    private double wage; `` 
    private int pieces;


    public void pieceWorker(String firstName, String lastName, String social,date dob, double wage, int pieces )  // use some getters ?
    {
        super(firstName,lastName,social, dob );
        setWage(wage);
        setPieces(pieces);
        this.wage = wage;
        this.pieces = pieces;
    }

您已為構造函數指定了void的返回類型。 由於構造函數不能具有返回類型,因此會收到此錯誤,因為您實際上尚未定義構造函數,它只是一個常規方法。 您需要在pieceWorker從方法中刪除void ,以使其成為構造函數,如下所示:

public pieceWorker(String firstName, String lastName, String social,Date dob, double wage, int pieces )
    {
        super(firstName,lastName,social, dob );
        setWage(wage);
        setPieces(pieces);
        this.wage = wage;
        this.pieces = pieces;
    }

另外,除非date是您創建的類,否則您可能需要在java.util包中將其更改為Date

由於父抽象類中沒有默認(或無參數)構造函數,因此必須指定子類中使用的構造函數。

在子類中,您沒有指定構造函數,因為您添加了返回類型void。 請如下修改代碼。

public abstract class Employee implements Payable
    private  String firstName;
    private  String lastName;
    private  String socialSecurityNumber;
    private date birthdate;

          // constructor
          public Employee(String firstName, String lastName,
             String social, date dob )
          {
             this.firstName = firstName;
             this.lastName = lastName;
            this.socialSecurityNumber = social;
             //this.birthdate = getBirthdate();
              // Birthdate(year, month, day);
              birthdate = dob;
          }



public class pieceWorker extends Employee  // no default constructor available
{
    private double wage; 
    private int pieces;


    public pieceWorker(String firstName, String lastName, String social,date dob, double wage, int pieces )  // use some getters ?
    {
        super(firstName,lastName,social, dob );
        setWage(wage);
        setPieces(pieces);
        this.wage = wage;
        this.pieces = pieces;
    }
}

正如@Gulllie和@dammina提到的(幾乎同時),class pieceWorker具有一個方法

public void pieceWorker(args...) 

而不是構造函數。 因此,pieceWorker類將使用默認構造函數。 任何構造函數的第一行都是對super()的調用。 如果您未顯式調用super(),則Java將為您完成此操作。

public abstract class Employee implements Payable{
private  String firstName;
private  String lastName;
private  String socialSecurityNumber;
private date birthdate;

      // constructor
      public Employee(String firstName, String lastName,
         String social, date dob )
      {
         this.firstName = firstName;
         this.lastName = lastName;
        this.socialSecurityNumber = social;
         //this.birthdate = getBirthdate();
          // Birthdate(year, month, day);
          birthdate = dob;
      }
}

您在上面的抽象類中沒有默認的構造函數,因為您定義了自己的構造函數。 編譯器將對pieceWorker類進行以下修改。

public class pieceWorker extends Employee  // no default constructor available
{
private double wage; 
private int pieces;

// compiler will insert following constructor
public pieceWorker(){
    super();
}

public void pieceWorker(String firstName, String lastName, String social,date dob, double wage, int pieces )  // use some getters ?
{
    super(firstName,lastName,social, dob );
    setWage(wage);
    setPieces(pieces);
    this.wage = wage;
    this.pieces = pieces;
}
}

您可以執行以下操作以使代碼正常工作:

public class pieceWorker extends Employee  // no default constructor available
{
private double wage; 
private int pieces;

public pieceWorker(String firstName, String lastName, String social,date dob, double wage, int pieces )  // use some getters ?
{
    super(firstName,lastName,social, dob );
    setWage(wage);
    setPieces(pieces);
    this.wage = wage;
    this.pieces = pieces;
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM