Java 方法不会重写或实现超类型中的方法
我正在使用继承和多态性创建一个模拟员工数据库。在尝试重写超类方法时,我遇到了以下错误Java 方法不会重写或实现超类型中的方法,java,inheritance,overriding,abstract,Java,Inheritance,Overriding,Abstract,我正在使用继承和多态性创建一个模拟员工数据库。在尝试重写超类方法时,我遇到了以下错误 HourlyEmployee is not abstract and does not override abstract method resetWeek() in Employee public class HourlyEmployee extends Employee ^ HourlyEmployee.java:43: error: method does not override or imple
HourlyEmployee is not abstract and does not override abstract method resetWeek() in Employee
public class HourlyEmployee extends Employee
^
HourlyEmployee.java:43: error: method does not override or implement a method from a supertype
@Override
^
HourlyEmployee.java:54: error: method does not override or implement a method from a supertype
@Override
^
HourlyEmployee.java:60: error: method does not override or implement a method from a supertype
@Override
^
HourlyEmployee.java:66: error: method does not override or implement a method from a supertype
@Override
这是我的Employee超类和HourlyEmployee子类代码
public abstract class Employee
{
protected String firstName;
protected String lastName;
protected char middleInitial;
protected boolean fulltime;
private char gender;
private int employeeNum;
public Employee (String fn, String ln, char m, char g, int empNum, boolean ft)
{
firstName = fn;
lastName = ln;
middleInitial = m;
gender = g;
employeeNum = empNum;
fulltime = ft;
}
public int getEmployeeNumber()
{
return employeeNum;
}
public void setEmployeeNumber(int empNum)
{
while (empNum <= 10000 && empNum >= 99999)
{
System.out.print ("Invalid input, please try again: ");
}
if (empNum >= 10000 && empNum <= 99999)
{
employeeNum = empNum;
}
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public char checkGender(char g)
{
if (g != 'M' || g != 'F')
{
g = 'F';
}
return g;
}
public char getGender()
{
return gender;
}
@Override
public boolean equals(Object e2)
{
if (this.employeeNum == ((Employee)e2).employeeNum)
{
return true;
}
else
{
return false;
}
}
@Override
public String toString()
{
return employeeNum + "\n" + lastName + ", " + firstName + "\n" + "Gender:" + gender + "\n" + "Status:" + fulltime + "\n";
}
public abstract double caclulateWeeklyPay();
public abstract void annualRaise();
public abstract double holidayBonus();
public abstract void resetWeek();
}
公共抽象类Employee
{
受保护的字符串名;
受保护的字符串lastName;
受保护字符首字母;
全职工作;
私人性别;
私人国际雇员;
公共雇员(字符串fn、字符串ln、字符m、字符g、int empNum、布尔ft)
{
firstName=fn;
lastName=ln;
中间初始值=m;
性别=g;
employeeNum=empNum;
全职=英尺;
}
public int getEmployeeNumber()
{
返回雇员;
}
public void setEmployeeNumber(int empNum)
{
而(empNum=99999)
{
System.out.print(“输入无效,请重试:”);
}
如果(empNum>=10000&&empNum 40)
{
w=w*2;
}
返回w*h;
}
//@凌驾
公共双年度加薪(双w)
{
返回值(w*.0.05)+w;
}
//@凌驾
公共双倍度假奖金(双倍w)
{
返回w*40;
}
//@凌驾
公共双周(双倍小时)
{
h=0.0;
返回h;
}
公共双脉冲工作(双h,双A)
{
而(h您的Employee类有4个抽象方法,没有一个实现(至少没有正确实现)。下面是一个示例:
double caclulateWeeklyPay(); // in Employee
double calculateWeeklyPay(double w, double h) // in HourlyEmployee
父类应包含相同的签名(包括参数),并且应如下所示:
public abstract double calculateWeeklyPay(double w, double h);
由于这似乎是家庭作业,我将把其余的留给您。请仔细阅读错误消息:
HourlyEmployee不是抽象的,并且不重写Employee中的抽象方法resetWeek()
它清楚地表明您的Employee
类是abstract
,并且有一个抽象方法resetWeek()
从Employee
扩展而来的类HourlyEmployee
不是抽象的,因此它应该覆盖超类的所有抽象方法,因为具体类不能有任何抽象方法
原因是HourlyEmployee
类中的方法与超类中的方法具有不同的签名。因此该方法不会被重写,并且编译器不接受@Override
注释,因为HourlyEmployee*类不是抽象的*您需要实现abst在EMPLOYEE抽象类中声明的ract方法。这一点非常明显。
您必须实现以下方法
public abstract double caclulateWeeklyPay();
public abstract void annualRaise();
public abstract double holidayBonus();
public abstract void resetWeek();
public abstract double caclulateWeeklyPay();
public abstract void annualRaise();
public abstract double holidayBonus();
public abstract void resetWeek();