gpt4 book ai didi

java - 继承代码没有返回值

转载 作者:行者123 更新时间:2023-12-01 17:16:29 24 4
gpt4 key购买 nike

我创建了一个名为 Employee 的父类(super class),以及一个名为 ProductionWorker 的子类和一个名为 ProductionWorkerDriver 的驱动程序。该程序获取我输入的信息并从 Employee 类返回 String 值,但不从 ProductionWorker 类返回 double 和 int 值。我正在使用的程序说我必须将这些值初始化为 0,我想这就是为什么它们在编译时显示为零的原因。但是,如果没有它,程序将无法编译,所以我不知道该怎么做。

public class Employee {
private String name;
private String employeeNumber;
private String hireDate;

public Employee(String n, String num, String date) {
name = n;
employeeNumber = num;
hireDate = date;
}

public Employee() {
name = "";
employeeNumber = "";
hireDate = "";
}

public void setName(String n) {
name = n;
}

public void setEmployeeNumber(String e) {
employeeNumber = e;
}

public void setHireDate(String h) {
hireDate = h;
}

public String getName() {
return name;

}

public String getEmployeeNumber() {
return employeeNumber;
}

public String getHireDate() {
return hireDate;
}

public String toString() {
String str = "Employee Name: " + name
+ "\nEmployee #: " + employeeNumber
+ "\nHire Date: " + hireDate;

return str;

}
} //end of Employee class


//beginning of ProductionWorker class
import java.text.DecimalFormat;

public class ProductionWorker extends Employee {
private int shift;
private double payRate;
public int DAY_SHIFT = 1;
public int NIGHT_SHIFT = 2;

public ProductionWorker(String n, String num, String date, int sh, double rate) {
super(n, num, date);
shift = sh;
payRate = rate;

}

public ProductionWorker() {

}

public void setShift(int s) {
shift = s;
}

public void setPayRate(double p) {
payRate = p;
}

public int getShift() {
return shift;
}

public double getPayRate() {
return payRate;
}

public String toString() {
DecimalFormat dollar = new DecimalFormat("#,##0.00");

String str = super.toString() + "\nShift: " + shift
+ "\nPay Rate: $" + dollar.format(payRate);

return str;
}
}//end of ProductionWorker class


//beginning of ProductionWorkerDriver
import java.util.Scanner;

public class ProductionWorkerDriver {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

String name = null;
String employeeNumber = null;
String hireDate = null;
int shift = 0;
double payRate = 0;
int DAY_SHIFT = 1;
int NIGHT_SHIFT = 2;

Employee info = new Employee(name, employeeNumber, hireDate);

System.out.println("Employee Name:");
name = keyboard.nextLine();

System.out.println("Employee #:");
employeeNumber = keyboard.nextLine();

System.out.println("Hire Date:");
hireDate = keyboard.nextLine();

ProductionWorker info2 = new ProductionWorker(name, employeeNumber, hireDate, shift, payRate);

System.out.println("Shift 1 or 2:");
shift = keyboard.nextInt();

System.out.println("Pay Rate:");
payRate = keyboard.nextDouble();

System.out.println(info2.toString());

}

}//end of ProductionWorkerDriver

最佳答案

您没有对输入的数据进行任何操作:

System.out.println("Shift 1 or 2:");
shift = keyboard.nextInt();

System.out.println("Pay Rate:");
payRate = keyboard.nextDouble();

...shiftpayRate 变量不再使用。更改这些本地变量的值不会更改您之前使用相同变量创建的实例中的值。

您应该调用:

info2.setShift(shift);
info2.setPayRate(payRate);

...或者更好的是,等到您提出这些问题之后再构建实例。您也根本没有使用 info 变量。您的整个 main 方法可以改进为:

Scanner keyboard = new Scanner(System.in);

System.out.println("Employee Name:");
String name = keyboard.nextLine();

System.out.println("Employee #:");
String employeeNumber = keyboard.nextLine();

System.out.println("Hire Date:");
String hireDate = keyboard.nextLine();

System.out.println("Shift 1 or 2:");
int shift = keyboard.nextInt();

System.out.println("Pay Rate:");
double payRate = keyboard.nextDouble();

ProductionWorker worker =
new ProductionWorker(name, employeeNumber, hireDate, shift, payRate);

System.out.println(worker);

请注意,在每种情况下,我们都是在需要变量时才声明它。

请注意,您的员工编号和雇用日期都是 String 值,这有点令人担心,而且使用 double 作为货币并不是一个好主意值 - 养成使用 BigDecimal 的习惯,或使用整数来表示美分/便士/其他数字。

关于java - 继承代码没有返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21767449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com