2016-09-22 64 views
0

由于静态方法和变量,我在代码中遇到了很多问题。 我是新来的编程,我注意到当我在课堂中使用静态时,我可以在任何我想要的地方调用它。 下面的链接是我以前的问题(如果你需要,它只是为什么不试图使用静态),其中有人告诉我为解决我的问题的一部分的方法删除静态。 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; 

} 
+1

你的getter和setter不应该是一成不变的,它们被用来获取或设置Car对象(实例)上的数据不上汽车类。如果你想流水线或简化你的汽车创作过程,请考虑使用Builder模式。如果你想在创建类的时候提供一些随机数据,把它移到你的Car类之外,例如创建一个CarDataProvider类并在那里添加这些静态函数。 –

回答

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

我不能它是像CR1或POS1字符串。 – Kavi

+0

并且系统应该是给它的系统 – Kavi

+0

您部分地发布了您的代码,所以我看不到您的类或主逻辑的字段。但是你可以解决它,只要停止尝试创建一个必须从该对象本身返回的信息的对象。如果您发布了所有代码,我可以帮助您提供确切的解决方案。 – Haddex

相关问题