2014-02-13 109 views
0

我創建了一個名爲Employee的超類,其子類叫ProductionWorker,驅動叫ProductionWorkerDriver。該程序接受我輸入的信息並返回Employee類中的字符串值,但不返回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 

回答

3

你沒有做任何事情與你輸入的數據:

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的,或者使用整數來表示一些美分/便士/無論。

+0

將ProductionWorker worker = new ...向下移動到println的下方似乎工作正常。此外,所有變量的數據類型均由我正在使用的教科書設置。感謝您的幫助! – user2970463

+0

@ user2970463:在這種情況下,您應該略微關注您的課本 - 除非它提到這僅僅是爲了簡單起見。 –

2

此:

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

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

將更新shift和你的主要方法payRate

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

因爲你從ProductionWorker輸出值,這些值是永遠更新,它將始終輸出0.簡單地說,因爲你初始化ProductionWorker這些變量並不意味着臨時t主要方法中的參考shift將指向與ProductionWorker內部的參考shift相同的內存位置。

+1

主方法中沒有'this.shift' - 它們是靜態方法中的局部變量。 'this'只與* instance *方法有關,在這種情況下'this.shift'將是一個* instance *變量。誤用術語只會進一步混淆事物。 –

+0

當然你是對的。我認爲那些是在課堂上宣佈的,而不是我第一次寫它的方法,並且沒有更新它。更新。 –