继承代码不返回任何值

时间:2014-02-13 23:02:39

标签: java inheritance

我创建了一个名为Employee的超类,其中包含一个名为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

2 个答案:

答案 0 :(得分:3)

您没有对输入的数据做任何事情:

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

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

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

您应该致电:

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,或使用整数来表示分数/便士/等等。

答案 1 :(得分:2)

此:

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

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

将更新主要方法的shiftpayRate

public static void main(String[] args)
{
    //...
    int shift = 0;
    double payRate = 0;

因为您正在输出ProductionWorker中的值,并且这些值永远不会更新,所以它将始终输出0.仅仅因为您使用这些变量初始化ProductionWorker并不意味着引用{ main方法中的{1}}将指向内存中与ProductionWorker中的引用shift相同的位置。