如何解决编译错误..类无法解析为类型
我创建了抽象类 Employee
并定义了一个名为 calculateSalary()
的方法作为抽象方法。 Employee 抽象类由 Contractor
& FullTimeEmployee
类继承。当我尝试在主类中实现 Abstract 时,出现编译器错误,显示类无法解析为类型。
-
Employee.Class
:
package Abstract;
public abstract class Employee
{
String name;
int salaryPerHour;
public Employee(String name,int salaryPerHour) {
this.name = name;
this.salaryPerHour = salaryPerHour;
}
public abstract int calculateSalary();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalaryPerHour() {
return salaryPerHour;
}
public void setSalaryPerHour(int salaryPerHour) {
this.salaryPerHour = salaryPerHour;
}
public class Contractor extends Employee
{
int workingHours;
public Contractor(String name,int salaryPerHour,int workingHours) {
super(name,salaryPerHour);
this.workingHours = workingHours;
}
@Override
public int calculateSalary()
{
return getSalaryPerHour() * workingHours;
}
}
public class FullTimeEmployee extends Employee
{
public FullTimeEmployee(String name,int salaryPerHour) {
super(name,salaryPerHour);
}
@Override
public int calculateSalary()
{
return getSalaryPerHour() * 8;
}
}
}
- 在
Main
类中:
package Abstract;
public class AbstractClassExample {
public static void main(String[] args) {
Employee contractor = new Contractor("contractor",10,10);
Employee fullTimeEmployee = new FullTimeEmployee("full time employee",8);
System.out.println(contractor.calculateSalary());
System.out.println(fullTimeEmployee.calculateSalary());
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。