2012-04-19 111 views
1

我正在用另一個單獨的驅動程序類創建一個類。汽車類是汽車租賃公司存儲有關汽車的信息,如品牌,型號和註冊號,以便使用我可以使用的駕駛員類輸入新車,檢查車輛是否出租和不可用,並命名聘用者的僱用者。創建一個Java Car類

我的賽車類方法:

public class Car { 

private String Make; 
private String Model; 
private int RegistrationNum; 

public Car(String Make, String Model, String RegN){ 
    //Constructor, 
    //stores the make, model and registration number of the new car 
    //sets its status as available for hire. 
    Make = ""; 
    Model = ""; 
    RegN = ""; 

} 

public String getMake(){ 
    return Make; 

} 

public String getModel(){ 
    return Model; 

} 

public boolean hire(String newHirer){ 

    { 
    //Hire this car to the named hirer and return true. 

     return true; 
    } 
    //Returns false if the car is already out on hire. 



} 

public boolean returnFromHire(){ 

    { 
//Receive this car back from a hire and returns true. 
     return true; 
    } 

//Returns false if the car was not out on hire  


} 

public int getRego(){ 


//Accessor method to return the car’s registration number  

    RegistrationNum++; 
    return RegistrationNum; 
    } 



public boolean hireable(){ 
//Accessor method to return the car’s hire status.  



    { 
//returns true if car available for hire   
    return true;  
    } 
} 

public String toString(){ 
//return car details as formatted string 
//output should be single line containing the make model and reg number 
//followed by either "Available for hire" or "On hire to: <name>" 


    return "Vehicle ID number: "+ getRego()+"-"+"Make is: "+ getMake()+"-"+"Model is: "+getModel(); 


} 


} 

以下是我Driver類:

import java.util.*; 
    public class CarDriver { 
    static Car car1; 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    Scanner scan = new Scanner(System.in); 


{ 
    System.out.println("Make?"); 
    String Make=scan.nextLine(); 
    System.out.println("Model"); 
    String Model=scan.nextLine(); 
    System.out.println("Registration number?"); 
    String RegNum=scan.nextLine(); 

    car1 = new Car(Make,Model,RegNum); 


    System.out.println("What you input :"); 

    System.out.println(car1.toString()); 
}} 

} 

我的輸出:

Make? 
carmake 
Model 
carmodel 
Registration number? 
12345t 
What you input : 
Vehicle ID number: 1-Make is: null-Model is: null 

問題:

  1. 無法理解如何將僞代碼轉換爲 布爾方法轉換成Java代碼

  2. 無法連接驅動 類來存儲信息,我輸入,如模型,使 和註冊號

+1

這是[標籤:家庭作業]? – 2012-04-19 09:29:22

+0

使用this.Makel,this.Model和this.red關鍵字在構造函數中存儲實例變量 – 2012-04-19 09:31:54

+0

done =]反正它其實不是作業,但修訂問題 – Liquified 2012-04-19 09:32:24

回答

3

第二

更改構造函數來此一:

public Car(String Make, String Model, String RegN){ 
    this.Make = Make; 
    this.Model= Model; 
    this.RegN = RegN; 
} 

你以前的構造函數有一個問題,基本上你所做的只是你得到了構造函數的參數,並將它們全部設置爲「」(空字符串),你不想這樣做。你想爲你的實例字段分配參數值。如果你想訪問實例字段,你必須使用關鍵字這個

public Car(String Make, String Model, String RegN){ 
//Constructor, 
//stores the make, model and registration number of the new car 
//sets its status as available for hire. 
Make = ""; 
Model = ""; 
RegN = ""; 

}

0

正如我在代碼中看到,您沒有設置汽車實例中的任何字段的構造。你應該寫水木清華這樣的:

public Car(String Make, String Model, String RegN){ 
    //Constructor, 
    //stores the make, model and registration number of the new car 
    //sets its status as available for hire. 
    this.Make = Make; 
    this.Model = Model; 
    this.RegN = RegN; 
} 
0

回答你的第二個問題:

你打電話的那一刻:

car1 = new Car(Make,Model,RegNum); 

Car類的構造函數在與變量一起叫你提供。 當你看看構造函數創建:

public Car(String Make, String Model, String RegN){   
    Make = "";  
    Model = "";  
    RegN = ""; 
}  

你可以看到你所提供的變量從未設置爲Car類的局部變量。 爲了解決這個問題,你需要改變它intto如下:

public Car(String Make, String Model, String RegN){   
    this.Make = Make;  
    this.Model = Model;  
    this.RegN = RegN; 
}  

在你的程序祝你好運!

1

第一:爲了檢索「這個汽車是誰僱用的」或「哪個司機有這輛汽車租用的信息」,您必須首先在店鋪那個信息。 你會使用什麼數據類型? (記住這是「家庭作業」,我認爲最好不要給我的答案)。

P.S:最好使用非大寫標識符來表示變量和非靜態/最終屬性。

0

當您使用所需的參數調用Car類的構造函數時,默認情況下,這些參數引用將發送給構造函數。

public Car(String Make, String Model, String RegN){   
    Make = "";  
    Model = "";  
    RegN = ""; 
} 
Car類的構造函數

您的參數和你Car類的成員變量具有相同的名稱。所以當構造函數被調用時,局部變量被改爲空字符串。由於本地變量包含來自調用者的引用,所以調用者中的原始變量也正在改變。 同樣,因爲您沒有爲您的類成員引用創建任何實例,它仍然爲空。 如果使用下面的代碼段在你的構造,

this.Make = "";  
this.Model = "";  
this.RegN = ""; 

Car類的成員變量將被改變,而不是從呼叫者的變量。