简体   繁体   English

方法不会覆盖或实现超类型的方法

[英]method does not override or implement a method from a supertype

I am creating a mock employee database using inheritance and polymorphism. 我正在使用继承和多态创建一个模拟员工数据库。 I am running into the following errors when trying to override the superclass methods. 尝试覆盖超类方法时遇到以下错误。

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

Here is my Employee Superclass and HourlyEmployee Subclass code 这是我的Employee Superclass和HourlyEmployee Subclass代码

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

Here is the HourlyEmployee subclass 这是HourlyEmployee子类

public class HourlyEmployee extends Employee
{
private double wage;
private double hoursWorked;
private double additionalHours;

public HourlyEmployee(String fn, String ln, char m, char g, int empNum, boolean ft, double w, double h, double ahours)
{
    super (fn, ln, m, g, empNum, ft);
    wage = w;
    hoursWorked = h;
    hoursWorked = 0.0;
    additionalHours = ahours;
}

@Override
public String toString()
{
    return this.getEmployeeNumber() + "\n" + lastName + ", " + firstName + middleInitial + "\n" + "Gender: "
     + this.getGender() + "\n" + "Status: " + fulltime + "\n" + "Wage: " + wage + "\n" + "Hours Worked: " + hoursWorked + "\n";
}

   //@Override    
public double calculateWeeklyPay(double w, double h)
{
    if (h > 40)
    {
        w = w * 2;
    }

    return w * h;        
}

//@Override
public double annualRaise(double w)
{
    return (w * .05) + w;
}

//@Override
public double holidayBonus(double w)
{
    return w * 40;
}

//@Override
public double resetWeek(double h)
{
    h = 0.0;
    return h;
}

public double plusHoursWorked(double h, double ahours)
{
    while (h <= 0)
    {
        System.out.println("Invalid value entered, please try again");
    }

    h += ahours;

    return h;
}


}

Your Employee class has 4 abstract methods, none of which are implemented (not properly at least). 您的Employee类有4个抽象方法,其中没有一个是实现的(至少没有正确实现)。 Here is an example: 这是一个例子:

double caclulateWeeklyPay(); // in Employee
double calculateWeeklyPay(double w, double h) // in HourlyEmployee

The parent class should contain the same signature (which includes parameters), and should look like this: 父类应包含相同的签名(包括参数),并且应如下所示:

public abstract double calculateWeeklyPay(double w, double h);

Since this appears to be homework, I will leave the rest to you. 由于这似乎是家庭作业,我会把剩下的留给你。

Just read the error message carefully: 只需仔细阅读错误消息:

HourlyEmployee is not abstract and does not override abstract method resetWeek() in Employee HourlyEmployee不是抽象的,不会覆盖Employee中的抽象方法resetWeek()

It clearly indicates that your Employee class is abstract and has an abstract method resetWeek() . 它清楚地表明您的Employee类是abstract并且有一个抽象方法resetWeek()

Your class HourlyEmployee which extends from Employee is not abstract, so it should override all abstract methods of the super class, as a concrete class can not have any abstract methods. Employee扩展的类HourlyEmployee不是抽象的,因此它应该覆盖超类的所有抽象方法,因为具体类不能有任何抽象方法。

The reason is that your methods in the HourlyEmployee class have a different signature than those in the super class. 原因是HourlyEmployee类中的方法与超类中的方法具有不同的签名。 So the method is not overridden, and the @Override annotation is not accepted by the compiler 因此,不会覆盖该方法,并且编译器不接受@Override注释

as your HourlyEmployee * class is not abstract * you need to implement the abstract methods declared in your EMPLOYEE abstract class . 因为您的HourlyEmployee * 类不是抽象的 *您需要实现在EMPLOYEE抽象类中声明的抽象方法 which is quite obvious. 这很明显。 you have to implement the below methods 你必须实现以下方法

 public abstract double caclulateWeeklyPay();

 public abstract void annualRaise();

 public abstract double holidayBonus();

 public abstract void resetWeek(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 方法不会覆盖或实现超类型的方法 - method does not override or implement a method from a supertype 方法不会从父类型@override覆盖或实现方法编译错误 - method does not override or implement a method from a supertype @override compile error 方法不会覆盖或实现超类型中的方法 - 用于覆盖 - method does not override or implement a method from a supertype - for Override 错误:方法未覆盖或从超类型实现方法 - error: method does not override or implement a method from a supertype 错误:方法未覆盖或从超类型OnCreateOptionsMenu实现方法 - error: method does not override or implement a method from a supertype OnCreateOptionsMenu 方法不会从超类型错误中覆盖或实现方法 - method does not override or implement a method from a supertype error React-Native方法不会覆盖或实现超类型的方法 - React-Native method does not override or implement a method from a supertype Java“方法没有覆盖或实现来自超类型的方法”错误 - Java "method does not override or implement a method from a supertype" error [Android] [RecyclerView]方法不会覆盖或实现超类型的方法 - [Android][RecyclerView] Method does not override or implement a method from a supertype 错误:(124,9)错误:方法未覆盖或从超类型实现方法 - Error:(124, 9) error: method does not override or implement a method from a supertype
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM