为什么 Eclipse 不使用 @Override?
Why wont Eclipse @Override?
我正在阅读 Java 傻瓜书,运行 遇到了一个问题。我不明白为什么 @Override 不起作用。我确定这与我的代码有关,因为我之前已经获得了一个多态数组来使用 override,但它太简单了,我无法模仿。
import static java.lang.System.out;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class DoPayrollTypeP {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner diskScanner = new Scanner(new File("EmpInfoNew.txt"));
Scanner kbdScanner = new Scanner (System.in);
for(int empNum = 1; empNum<=3; empNum++){
payOneFTEmployee (diskScanner);
}
for(int empNum = 4; empNum<=6; empNum++){
payOnePTEmployee(diskScanner, kbdScanner);
}
}
public static void payOneFTEmployee(Scanner diskScanner){
FullTimeEmployee ftemployee = new FullTimeEmployee();
ftemployee.setName(diskScanner.nextLine());
ftemployee.setJobTitle(diskScanner.nextLine());
ftemployee.setWeeklySalary(diskScanner.nextDouble());
ftemployee.setBenefitDeduction(diskScanner.nextDouble());
diskScanner.nextLine();
diskScanner.nextLine();
ftemployee.cutCheck(ftemployee.findPaymentAmount());
out.println();
}
public static void payOnePTEmployee(Scanner diskScanner, Scanner kbdScanner) {
PartTimeEmployee ptemployee = new PartTimeEmployee();
ptemployee.setName(diskScanner.nextLine());
ptemployee.setJobTitle(diskScanner.nextLine());
ptemployee.setHourlyRate(diskScanner.nextDouble());
diskScanner.nextLine();
diskScanner.nextLine(); //Reads the dashed line that
// separates two employees
out.print("Enter ");
out.print(ptemployee.getName());
out.print("'s hours worked this week: ");
int hours = kbdScanner.nextInt();
ptemployee.cutCheck(ptemployee.findPaymentAmount(hours));
out.println();
}
}
下一个class:
import static java.lang.System.out;
public class Employee {
private String name;
private String jobTitle;
public void setName(String nameIn) {
name = nameIn;
}
public String getName() {
return name;
}
public void setJobTitle(String jobTitleIn) {
jobTitle = jobTitleIn;
}
public String getJobTitle() {
return jobTitle;
}
public void cutCheck(double amountPaid){
out.printf("Pay to the order of %s", name);
out.printf("(%s) ***$", jobTitle);
out.printf("%,.2f\n", amountPaid);
}
}
下一个Class:
public class PartTimeEmployee extends Employee{
private double hourlyRate;
public void setHourlyRate(double hourlyRateIn) {
hourlyRate = hourlyRateIn;
}
public double getHourlyRate() {
return hourlyRate;
}
public double findPaymentAmount(int hours){
return hourlyRate * hours;
} //method that should be overriden
}
Class 应该覆盖:
public class PartTimeWithOver extends PartTimeEmployee{
@Override
public double findPaymentAmount(int hours) {
if(hours <= 40) {
return getHourlyRate() * hours;
} else {
return getHourlyRate() * 40 +
getHourlyRate() * 2 * (hours - 40);
}
}
}
EmpInfoNew.(txt) 文件
jo shmo
首席执行官
5000.00
500.00
edd shmoe
队长
5000.00
500.00
鲍勃·肖莫
名誉执行官
1000.00
200.00
戴夫·肖莫
driver
7.25
等等等等
厨师
8.50
len shmo
厨房主管
12.50
您已将对象实例化为
PartTimeEmployee ptemployee = new PartTimeEmployee();
它是 PartTimeWithOther
的 base
class,所以它不能调用 derived
class 中的 overridden
方法。
改为
PartTimeEmployee ptemployee = new PartTimeWithOver();
Here 是一个很好的教程,其中的图表解释了 多态性
您从不使用 PartTimeWithOver,您只使用 PartTimeEmployee。
因此永远不会调用您重写的方法。
我正在阅读 Java 傻瓜书,运行 遇到了一个问题。我不明白为什么 @Override 不起作用。我确定这与我的代码有关,因为我之前已经获得了一个多态数组来使用 override,但它太简单了,我无法模仿。
import static java.lang.System.out;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class DoPayrollTypeP {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner diskScanner = new Scanner(new File("EmpInfoNew.txt"));
Scanner kbdScanner = new Scanner (System.in);
for(int empNum = 1; empNum<=3; empNum++){
payOneFTEmployee (diskScanner);
}
for(int empNum = 4; empNum<=6; empNum++){
payOnePTEmployee(diskScanner, kbdScanner);
}
}
public static void payOneFTEmployee(Scanner diskScanner){
FullTimeEmployee ftemployee = new FullTimeEmployee();
ftemployee.setName(diskScanner.nextLine());
ftemployee.setJobTitle(diskScanner.nextLine());
ftemployee.setWeeklySalary(diskScanner.nextDouble());
ftemployee.setBenefitDeduction(diskScanner.nextDouble());
diskScanner.nextLine();
diskScanner.nextLine();
ftemployee.cutCheck(ftemployee.findPaymentAmount());
out.println();
}
public static void payOnePTEmployee(Scanner diskScanner, Scanner kbdScanner) {
PartTimeEmployee ptemployee = new PartTimeEmployee();
ptemployee.setName(diskScanner.nextLine());
ptemployee.setJobTitle(diskScanner.nextLine());
ptemployee.setHourlyRate(diskScanner.nextDouble());
diskScanner.nextLine();
diskScanner.nextLine(); //Reads the dashed line that
// separates two employees
out.print("Enter ");
out.print(ptemployee.getName());
out.print("'s hours worked this week: ");
int hours = kbdScanner.nextInt();
ptemployee.cutCheck(ptemployee.findPaymentAmount(hours));
out.println();
}
}
下一个class:
import static java.lang.System.out;
public class Employee {
private String name;
private String jobTitle;
public void setName(String nameIn) {
name = nameIn;
}
public String getName() {
return name;
}
public void setJobTitle(String jobTitleIn) {
jobTitle = jobTitleIn;
}
public String getJobTitle() {
return jobTitle;
}
public void cutCheck(double amountPaid){
out.printf("Pay to the order of %s", name);
out.printf("(%s) ***$", jobTitle);
out.printf("%,.2f\n", amountPaid);
}
}
下一个Class:
public class PartTimeEmployee extends Employee{
private double hourlyRate;
public void setHourlyRate(double hourlyRateIn) {
hourlyRate = hourlyRateIn;
}
public double getHourlyRate() {
return hourlyRate;
}
public double findPaymentAmount(int hours){
return hourlyRate * hours;
} //method that should be overriden
}
Class 应该覆盖:
public class PartTimeWithOver extends PartTimeEmployee{
@Override
public double findPaymentAmount(int hours) {
if(hours <= 40) {
return getHourlyRate() * hours;
} else {
return getHourlyRate() * 40 +
getHourlyRate() * 2 * (hours - 40);
}
}
}
EmpInfoNew.(txt) 文件
jo shmo 首席执行官 5000.00
500.00
edd shmoe 队长 5000.00
500.00
鲍勃·肖莫 名誉执行官 1000.00
200.00
戴夫·肖莫 driver
7.25
等等等等 厨师
8.50
len shmo 厨房主管
12.50
您已将对象实例化为
PartTimeEmployee ptemployee = new PartTimeEmployee();
它是 PartTimeWithOther
的 base
class,所以它不能调用 derived
class 中的 overridden
方法。
改为
PartTimeEmployee ptemployee = new PartTimeWithOver();
Here 是一个很好的教程,其中的图表解释了 多态性
您从不使用 PartTimeWithOver,您只使用 PartTimeEmployee。
因此永远不会调用您重写的方法。