“在抽象类中没有默认构造函数”

问题描述:

我得到了一个没有默认构造函数可用的错误,只有一个具体类实现了相同的抽象类,我不知道为什么,任何帮助都非常感谢。“在抽象类中没有默认构造函数”

`

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; 
    } 
+1

你延伸的没有一个无参数的构造函数的类,所以你需要调用与超级构造正确的参数('super(...);') – Rogue

+2

pieceWorker需要删除之前的'void'。 –

+0

@CarlMastrangelo是正确的,它是你的语法!构造函数不需要返回类型..他们总是返回自己的实例 – Sagar

您所指定的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是你创建一个类时,你可能想将其更改为Date,在java.util包中。

由于在父级抽象类中没有缺省(或无参数)构造函数,因此必须指定子类中使用的构造函数。

在子类中,您没有指定构造函数,因为您添加了返回类型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(几乎在同一时间),类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; 
} 
}