我正在用另一个单独的驱动程序类创建一个类。汽车类是供租车公司存储有关汽车的信息,例如品牌、型号和注册号,以便使用我可以用来输入新车辆的驾驶员类,检查车辆是否在租用和不可用以及名称雇用者如果被雇用。
我的汽车类方法:
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
问题:
无法理解如何将伪代码转换为 java代码中的 boolean 方法
无法连接驱动程序 类来存储我输入的信息,比如模型,make 和注册号
最佳答案
第二
将构造函数改成这个:
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 = "";
关于java - 创建 Java 汽车类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10225224/