创建 Java Car 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10225224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:06:51  来源:igfitidea点击:

Creating a Java Car class

javainheritance

提问by Liquified

I'm making a class with another separate driver class. The car class is for a Car Hire Company to store the information about the car like the make, model and registration number, so that using the driver class I can use to input new vehicles, check if a vehicle is on hire and unavailable and name of hirer if it is hired.

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

My car class with methods:

我的汽车课有方法:

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


}


}

Following is my Driver class:

以下是我的驱动程序类:

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

 }

my output:

我的输出:

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

Problems:

问题:

  1. unable to understand how to convert the pseudocode for the boolean methods into java codes

  2. unable to connect the driver class to store the information that I input, like the model, make and registration number

  1. 无法理解如何将布尔方法的伪代码转换为 java 代码

  2. 无法连接驱动程序类来存储我输入的信息,如型号、品牌和注册号

回答by jellyfication

2nd

第二

Change constructor to this one:

将构造函数更改为:

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

Your previous contructor had a problem, basically all you did was that you get the constructor arguments and set all of them to "" (empty string), you dont want to do that. You want to assign argument values to your instance fields. If you want to access instance fields you have to use keyword this.

您之前的构造函数有问题,基本上您所做的就是获取构造函数参数并将它们全部设置为“”(空字符串),您不想这样做。您想为实例字段分配参数值。如果要访问实例字段,则必须使用关键字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 = "";

}

}

回答by J.A.I.L.

1st: In order to retrieve information about "whom is this car hired to" or "which driver has this car hired", you must storethat information first. What data type would you use? (remember this are "homeworks", I think it's better not to give myanswer).

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

P.S: it's better to use non capitalized identifiers for variables and non-static/final attributes.

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

回答by Saikat

When you are calling the constructor of Carclass with your desired parameters, by default those parameter reference are sent to the constructor.

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

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

in the Carclass constructor your parameters and member variables of your Carclass have the same name. So when the constructor is called, the local variables are changed to empty string. And as the local variable contains the reference from the caller, so the original variables in the caller is also being changed. Again as you are not creating any instance to your class member reference, it still remains null. If you use the following code segment in your constructor,

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

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

Carclass member variables will be changed instead of the variables from the caller.

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

回答by wizardzloy

As I see in the code, you don't set any fields of Car instance in the constructor. You should write smth like this:

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

回答by Byron Voorbach

Answer to your second question:

回答你的第二个问题:

The moment you call:

你打电话的那一刻:

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

The constructor of the Car class is called upon along with the variables you supplied. When you take a look at the constructor you created:

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

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

You can see that the variables you provided are never set to the local variables of the Car class. In order to fix this you need to change it intto the following:

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

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

Good luck on your program!

祝你的节目好运!