我只在一个实现相同抽象类的具体类中得到一个没有默认构造函数的错误,我不确定为什么,任何帮助都非常感激。
`
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;
}
答案 0 :(得分:2)
您为构造函数指定了返回类型void
。因为构造函数不能有返回类型,所以你得到这个错误,因为你实际上没有定义构造函数,它只是一个常规方法。
您需要从void
中的方法中删除pieceWorker
,使其成为构造函数,如下所示:
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
。
答案 1 :(得分:2)
由于父抽象类中没有默认(或无参数)构造函数,因此必须指定子类中使用的构造函数。
在子类中,您没有指定构造函数,因为您已添加返回类型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;
}
}
答案 2 :(得分:1)
正如@Gulllie和@dammina(几乎在同一时间)提到的那样,class pieceWorker有一个方法
public void pieceWorker(args...)
而不是构造函数。因此,class 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;
}
}