2016-09-22 79 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