【发布时间】:2016-09-23 07:45:52
【问题描述】:
我有一个汽车类,它的构造函数如下所示:
public Cars(String carID, String plateNum, String position, Attendant assignedTo, long currTime) {
this.carID = carID;
this.plateNum = plateNum;
this.position = position;
this.assignedTo = assignedTo;
this.currTime = currTime;
}
当我从菜单中按下创建汽车时,构造函数在另一个类(主)中初始化。
构造函数的参数都是由汽车类中的方法创建/初始化的。系统应该是提供信息的系统,例如(ID、位置和时间)。
问题是汽车对象还没有被初始化,所以我不能让方法工作。
但我确实需要汽车对象来包含其信息(如汽车 ID:CR1)。
信息都是字符串,时间除外。
我该怎么做?
PS:我是编程新手。起初我使用的是静态方法,但结果发现静态方法给我的代码带来了另一个更大的麻烦。
我在有我的静态方法的地方发布了一些东西,每个人都告诉我删除静态方法。
public void addCar() {
Cars car1 = new Cars(car1.getID(), car1.askCarID(), Cars.getPosition(), Attendant.askForAtt(), System.currentTimeMillis());
myGarage.add(car1);
if(!(car1.getAssignedTo()).equals(null)){
car1.getAssignedTo().setAssign(car1);
car1.getAssignedTo().setAvailable(false);
}
}
当我想创建一辆新车时,这就是所谓的。
我还放了整个汽车类以备不时之需:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import javax.swing.text.Position;
public class Cars {
private String carID;
private String plateNum;
private String position;
private Attendant assignedTo;
private long currTime;
static ArrayList<String> tempArray2 = new ArrayList<String>();
public Cars(String carID, String plateNum, String position, Attendant assignedTo, long currTime) {
this.carID = carID;
this.plateNum = plateNum;
this.position = position;
this.assignedTo = assignedTo;
this.currTime = currTime;
}
public void createCarsID() {
for (int x = 0; x < Garage.getCarsCapacity(); x++) {
String tempCarID = ("CR" + (x + 1));
tempArray2.add(tempCarID);
}
}
public String getID() {
createCarsID();
String tempID = null;
String tempPos = null;
for (int x = 0; x < Garage.getCarsCapacity(); x++) {
if (tempArray2.get(x) != null) {
tempID = tempArray2.get(x);
tempPos = tempArray2.get(x);
tempArray2.remove(tempArray2.get(x));
getPos(tempPos);
//tempArray2.get(x) = null;
break;
}
}
return tempID;
}
public static void getPos(String IdToPos) {
String strPos = IdToPos.substring(2);
int pos = Integer.parseInt(strPos);
position = "GR" + pos;
}
public String getPlateNum() {
return plateNum;
}
public static String getCarID() {
return carID;
}
public static String getPosition() {
return position;
}
public long getCurrTime() {
return currTime;
}
public Attendant getAssignedTo() {
return assignedTo;
}
public static String askCarID() {
boolean valid = false;
System.out.println("Please enter your car's plate number.");
Scanner scanCarID = new Scanner(System.in);
String scannedCarID = scanCarID.nextLine();
while (!valid) {
if (scannedCarID.matches("^[A-Za-z][A-Za-z] [0-9][0-9][0-9]$")) {
valid = true;
System.out.println(scannedCarID);
} else {
System.out.println("Please enter a valid plate number. Ex: AF 378");
askCarID();
}
}
return scannedCarID.toUpperCase();
}
public String convert(long miliSeconds) {
int hrs = (int) TimeUnit.MILLISECONDS.toHours(miliSeconds) % 24;
int min = (int) TimeUnit.MILLISECONDS.toMinutes(miliSeconds) % 60;
int sec = (int) TimeUnit.MILLISECONDS.toSeconds(miliSeconds) % 60;
return String.format("%02d:%02d:%02d", hrs, min, sec);
}
@Override
public String toString() {
return "Car:" + plateNum + " ID:" + carID + " Position:" + position + " Assigned to:" + assignedTo.getId()
+ "(" + assignedTo.getName() + ")" + " Parked at:" + convert(currTime);
}
}
【问题讨论】:
-
"系统应该是提供诸如(ID、位置和时间)等信息的系统。" - 这里的“系统”是什么意思?在我看来,您只想向用户请求所有信息,然后 然后 调用构造函数。对于您只是询问汽车信息的地方来说,使用静态方法很有可能很好 - 毕竟这不需要任何先前的状态。
-
不要把问题的第一句话说成“英语不是我的母语”。它妨碍了。将您的第一句话作为问题的摘要。
-
“不要在你的问题的第一句话说“英语不是我的母语。”“尤其是因为你的英语很好。
-
也许你在这里尝试做的一些背景?
addCar()方法与您当前代码中的方法相同吗?该方法有几个错误.. -
我的意思是我的代码应该是添加诸如汽车ID之类的信息而不是用户(来自菜单)的代码
标签: java constructor static