[英]Returning the child of an abstract class in Java?
I've been working on a program for staff management, and currently I have a class (HR), an abstract Employee class, with 2 child classes (Manager and SalesPerson). 我一直在开发一个用于员工管理的程序,目前我有一个班级(HR),一个抽象的Employee班级,有2个子班级(Manager和SalesPerson)。 Currently in the HR function, I have a function that should return a type of employee dependent on the user's input, but currently get the error
当前在HR函数中,我有一个函数应该根据用户的输入返回一种员工类型,但是当前会收到错误
incompatible types: unexpected return value
Here is the code the other classes. 这是其他类的代码。
HR (Function snippet, rather than whole class) HR(功能片段,而不是整个课程)
public Employee createNewEmployee(int index)
{
switch(index)
{
case 0:
idTracker++;
return new Manager(idTracker, nameField.getText(), Double.parseDouble(salaryField.getText()));
break;
case 1:
idTracker++;
return new SalesPerson(idTracker, nameField.getText(), Double.parseDouble(salaryField.getText()));
break;
default:
System.out.println("Didn't work");
}
}
Employee class 员工阶层
public abstract class Employee
{
protected double salary;
protected String name;
protected int IDNumber;
protected String jobTitle;
protected String dateEmployed;
public Employee(int ID, String n, double s)
{
IDNumber = ID;
setName(n);
setSalary(s);
}
}
And a Salesperson class (manager class is identical, but has a difference in the string jobTitle to show its a manager) 还有一个Salesperson类(经理类是相同的,但是在字符串jobTitle中有所不同,以显示其经理)
public class SalesPerson extends Employee
{
public SalesPerson(int ID, String n, double s)
{
super(ID, n, s);
jobTitle = "Sales Person";
}
}
Any method should always return a value of the declared return type. 任何方法都应始终返回声明的返回类型的值。 But there exists in your function
createNewEmployee
an execution path that do not return any value, compiler then tries to emit a simple return, alas this is not compatible with the declared Employee
type. 但是您的函数
createNewEmployee
存在一个不返回任何值的执行路径,编译器随后尝试发出简单的返回,因为这与声明的Employee
类型不兼容。 You can add a return null
in the default case, for example. 例如,您可以在默认情况下添加
return null
。
Note that createNewEmployee
is unusually badly named, create implies new , then name it createEmployee
or newEmployee
(the first is the more usual case). 请注意,
createNewEmployee
名称异常错误, create暗示new ,然后将其命名为createEmployee
或newEmployee
(第一种是更常见的情况)。
If the error occurs in one of the lines where a new instance is returned (eg return new Manager(...)
), it might indicate that the Employee
class expected as the return value is not the same type as the Employee
class which is extended by the two subclasses SalesPerson
and Manager
. 如果在返回一个新的实例的线路之一发生错误(例如,
return new Manager(...)
这可能表明该Employee
类预期的那样返回值是不一样的类型与Employee
类,这是由SalesPerson
和Manager
的两个子类扩展。
Check the imports in the HR class if the Employee
return value is the same as the Employee
class extended by the two subclasses. 如果
Employee
返回值与两个子类扩展的Employee
类相同,请检查HR类中的导入。 Maybe you imported an Employee
class with the same name but from a different package. 也许您从另一个包中导入了具有相同名称的
Employee
类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.