由于静态方法和变量,我的代码中存在很多问题。 我是编程的新手,我注意到当我在课堂上使用静态时,我可以在任何我想要的地方调用它。 下面的链接是我之前的问题(如果你需要,它只是为什么尝试不使用静态)有人告诉我删除静态的方法解决了我的问题。
PS:英语是我的第三语言,所以可能不好。 但请帮助我,我被困了
Get random object from arraylist specific variable
所以我的问题:
我有一个构造函数来创建汽车,参数填充了函数。
但是,由于尚未创建的对象,我无法获得这些方法。
有没有办法我打电话给他们?
这是我的代码:
现在Cars.getPosition()
用红色加下划线,它告诉我让它静止。
从创建汽车的车库类:
public void addCar() {
Cars car = new Cars(Cars.getID(), Cars.askCarID(), Cars.getPosition(), Attendant.askForAtt(), System.currentTimeMillis());
myGarage.add(car);
if(!(car.getAssignedTo()).equals(null)){
car.getAssignedTo().setAssign(car);
car.getAssignedTo().setAvailable(false);
}
}
从汽车类:
private static void createCarsID() {
for (int x = 0; x < Garage.getCarsCapacity(); x++) {
String tempCarID = ("CR" + (x + 1));
tempArray2.add(tempCarID);
}
}
public static 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 void getPos(String IdToPos) {
String strPos = IdToPos.substring(2);
int pos = Integer.parseInt(strPos);
position = "GR" + pos;
}
答案 0 :(得分:0)
这只是你可以做到的方法之一。新车的信息来自键盘,但不一定要这样。
Garage.java
package yourPackage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Víctor Bernabé Pérez
*/
public class Garage {
private final Scanner scanInt = new Scanner(System.in);
private final InputStreamReader inStream = new InputStreamReader(System.in);
private final BufferedReader scanText = new BufferedReader(inStream);
private final ArrayList<Car> cars;
public Garage() {
cars = new ArrayList<>();
}
public void addCar() {
try {
System.out.print("id");
int id = scanInt.nextInt();
System.out.print("carId");
int carId = scanInt.nextInt();
System.out.print("position");
int position = scanInt.nextInt();
long timeMillis = System.currentTimeMillis();
System.out.print("Attendant");
String attendant = new String();
try {
attendant = scanText.readLine();
} catch (IOException ex) {
Logger.getLogger(Garage.class.getName()).log(Level.SEVERE, null, ex);
}
boolean free;
String available = scanText.readLine();
if (available.equals("s")) {
free = true;
} else {
free = false;
}
cars.add(new Car(id, carId, position, attendant, timeMillis, free));
} catch (IOException ex) {
Logger.getLogger(Garage.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Car.java
package yourPackage;
/**
*
* @author Víctor Bernabe Pérez
*/
public class Car {
private int id, carId, position;
private long timeMillis;
private String attendant;
private boolean available;
public Car(){
//Nothing, just default constructor
}
public Car(int id, int carId, int position, String attendant, long timeMillis, boolean available){
this.id = id;
this.carId = carId;
this.position = position;
this.attendant = attendant;
this.timeMillis = timeMillis;
this.available = available;
}
}