2012-04-19 107 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类的成员变量将被改变,而不是从呼叫者的变量。