【问题标题】:Creating a Java Car class创建 Java Car 类
【发布时间】:2012-04-30 19:10:50
【问题描述】:

我正在使用另一个单独的驱动程序类创建一个类。 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();


}


}

以下是我的 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. 无法连接驱动程序 类来存储我输入的信息,比如模型,制作 和注册号

【问题讨论】:

  • 在构造函数中使用 this.Make,this.Model 和 this.red 关键字存储到实例变量中
  • done =] 无论如何它实际上不是作业,而是修订问题
  • 如果你的变量名不大写,你的代码看起来会好很多。

标签: java inheritance


【解决方案1】:

第二次

将构造函数更改为这个:

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

}

【讨论】:

    【解决方案2】:

    正如我在代码中看到的,您没有在构造函数中设置 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;
    }
    

    【讨论】:

      【解决方案3】:

      回答你的第二个问题:

      你打电话的那一刻:

      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;   
      }    
      

      祝你的计划好运!

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

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

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

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

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-10-01
            • 2016-12-05
            • 2012-05-03
            • 2011-04-05
            • 2016-10-24
            • 2013-01-31
            相关资源
            最近更新 更多