简体   繁体   English

Java - 无法从子类对象访问抽象方法

[英]Java - Cannot Access Abstract Method from Subclass Object

I'm working on a Java project where I have a superclass called Personnel , and two subclasses;我正在开发一个 Java 项目,其中有一个名为Personnel的超类和两个子类; Manager and Worker . ManagerWorker

The subclasses contain separate variables that are used to hold information about their pay, and I previously had method getSalary() for Manager and getWage() for Worker .子类包含单独的变量,用于保存有关他们的工资的信息,我以前有方法getSalary()用于ManagergetWage()用于Worker However, I wanted a number of these to be accessible through a for-loop so I put an abstract method into the Personnel class called getAnnualIncome() and put a corresponding method into both subclasses:但是,我希望可以通过 for 循环访问其中的一些,因此我将一个抽象方法放入名为getAnnualIncome()Personnel类中,并将相应的方法放入两个子类中:

public abstract class Personnel
{
//attributes of each instance of class
protected String name;
protected int payrollNum;
protected String dept;

//abstract method to be defined in subclasses
public abstract float getAnnualIncome();
}

In Manager :Manager

public float getAnnualIncome()
{
    //convert salary variable to real number
    float salaryAsFloat = salary;

    //return real number
    return salaryAsFloat;
}

In Worker :Worker

public float getAnnualIncome()
{
    //number of weeks in a year as constant value
    final int weeksInYear = 52;

    //weekly wage by number of weeks in a year
    return ((hourlyRate * hoursPerWeek) * weeksInYear);
}

This all works fine in theory, but when I came to actually implementing the for-loop I mentioned (using an array of the Personnel class with objects from both subclasses as elements), the following piece of code did not recognize the getAnnualIncome() method (this is taken from the executable main class):这在理论上一切正常,但是当我开始实际实现我提到的 for 循环时(使用Personnel类的数组,将两个子类的对象作为元素),以下代码无法识别getAnnualIncome()方法(这是从可执行主类中获取的):

for (int index = 0; index < employee.length; ++index)
{
    System.out.printf(employee[index].getName()
    + "\t\t" + "£%.2f %n", employee[index].getAnnualIncome());
                          //error message appears here ^^ 'method is undefined in 'Personnel'
}

Not really sure what's going wrong here, thought I'd covered everything that needed to be done.不太确定这里出了什么问题,以为我已经涵盖了所有需要做的事情。 As you can see from my code extracts, the method is in the superclass!从我的代码摘录中可以看出,该方法在超类中!

I don't really know where to go from here, any shove in the right direction would be greatly appreciated!我真的不知道从哪里开始,任何朝正确方向的推动将不胜感激!

Thanks, Mark谢谢,马克

Are you able to create an array of the Personnel class with objects from both subclasses as elements?您是否能够使用来自两个子类的对象作为元素创建 Personnel 类的数组? because that itself will give you error.因为这本身会给你错误。 "Can not convert subclass(Worker/ Manager) to Personnel[]. “无法将子类(工人/经理)转换为人员 []。

In case that works fine for you, than the problem is with the annotation @Override.如果这对您来说效果很好,那么问题就出在 @Override 注释上。

When you made an abstract class method, you are telling the JVM, "Childs" from this "Parent" WILL have those methods, and will either perform them, or have their "Childs" perform them.当你创建一个抽象类方法时,你是在告诉 JVM,来自这个“父”的“子”将拥有这些方法,并且要么执行它们,要么让它们的“子”执行它们。

Absctract class methods cannot use Objects details in themselfs, they are not able to be instantiated.抽象类方法本身不能使用对象的细节,它们不能被实例化。

Either have getAnnualIncome() in both Worker and Manager (and their "Children") or have that abstract class do the method.要么在WorkerManager (以及他们的“孩子”)中都有getAnnualIncome()要么让那个抽象类做这个方法。

To fix your issue, use the annotation @Override, to inform Java 6+ that the code is legitimated (while optional, this could help), and check that the hierarchy is correct要解决您的问题,请使用注释 @Override,通知 Java 6+ 代码是合法的(虽然是可选的,但这可能会有所帮助),并检查层次结构是否正确

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM