1

我正在用另一个单独的驱动程序类创建一个类。car 类用于汽车租赁公司存储有关汽车的信息,如品牌、型号和注册号,以便使用 driver 类我可以用来输入新车,检查车辆是否正在出租和不可用以及名称租用者,如果它被雇用。

我的汽车类有方法:

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();


}


}

以下是我的驱动程序类:

  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. 无法连接驱动类来存储我输入的信息,比如型号、品牌和注册号

4

5 回答 5

3

第二

将构造函数更改为此:

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

您以前的构造函数有一个问题,基本上您所做的只是获取构造函数参数并将它们全部设置为“”(空字符串),您不想这样做。您想将参数值分配给您的实例字段。如果要访问实例字段,则必须使用关键字this

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 = "";

}

于 2012-04-19T09:32:09.600 回答
1

第一:为了检索有关“这辆车租给谁”或“哪个司机租了这辆车”的信息,您必须首先存储该信息。你会使用什么数据类型?(记住这是“作业”,我认为最好不要给出我的答案)。

PS:最好对变量和非静态/最终属性使用非大写标识符。

于 2012-04-19T09:59:18.330 回答
0

当您Car使用所需参数调用类的构造函数时,默认情况下这些参数引用将发送到构造函数。

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

Car类构造函数中,您的类的参数和成员变量Car具有相同的名称。所以当构造函数被调用时,局部变量变成了空字符串。并且由于局部变量包含来自调用者的引用,所以调用者中的原始变量也在被改变。同样,由于您没有为您的类成员引用创建任何实例,它仍然保持为空。如果您在构造函数中使用以下代码段,

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

Car类成员变量将被更改,而不是来自调用者的变量。

于 2014-07-23T08:15:12.557 回答
0

回答你的第二个问题:

你打电话的那一刻:

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

Car 类的构造函数与您提供的变量一起被调用。当您查看您创建的构造函数时:

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

您可以看到您提供的变量从未设置为 Car 类的局部变量。为了解决这个问题,您需要将其更改为以下内容:

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

祝你的节目好运!

于 2012-04-19T09:38:40.633 回答
0

正如我在代码中看到的,您没有在构造函数中设置 Car 实例的任何字段。你应该这样写:

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;
}
于 2012-04-19T09:37:50.120 回答