2016-09-22 117 views
0

我有2個類。 CarAttendant從另一個類中調用非靜態方法

public Attendant(int staffNum, String id, boolean available, attNm name, Cars assign) { 
    this.staffNum = staffNum; 
    this.id = id; 
    this.available = available; 
    this.name = name; 
    this.assign = assign; 
} 

public Cars(String carID, String plateNum, String position, Attendant assignedTo, long currTime) { 
    this.carID = carID; 
    this.plateNum = plateNum; 
    Cars.position = position; 
    Cars.assignedTo = assignedTo; 
    this.currTime = currTime; 
} 

我attandants與創建一個for循環:

createIDList(); 
int staffAmount = getStaffAmount(); 

for (int x = 0; x < staffAmount; x++) { 
    Attendant att = new Attendant(x + 1, Attendant.tempArray[x], true, attNm.getNm(), null); 
    myAtt.add(att); 
} 
for (int x = 0; x < myAtt.size(); x++) { 
    System.out.println(myAtt.get(x)); 
} 

所有參數都從中得到他們從ArrayList的功能。所有的服務員都有相同的名字。

這與汽車是一樣的。 每次我創建了一個車,它的功能是和汽車都會有自己的名字:汽車

當他們創建了服務員,我的車都在其ArrayList(ArraylistCar和arraylistAtt)

但是當加入我正在創建新車,所有車輛都使用相同的ID輸出,並且錯誤地輸出了服務人員的詳細信息。

有人告訴我,靜態方法和變量是造成這個問題的原因。所以我試圖刪除一些靜態,但我得到錯誤。 如果我使用getID方法刪除靜態,我無法在助理類中調用它。 而且當我用方法創建一個新的類作爲參數時,這些情況也會發生(方法不再是靜態的,所以我不能從其他類調用它們)。

請幫我我卡住了。我知道它很長時間我嘗試了不同的方法,但沒有任何工作。 我也把我的toString()方法,以防萬一他們的問題。 有些人告訴我也把我的整個代碼和我的輸出,所以如果你需要它完全在下面。

車:

@Override 
public String toString() { 
    return "Car:" + plateNum + " ID:" + carID + " Position:" + position + " Assigned to:" + assignedTo.getId() 
    + "(" + assignedTo.getName() + ")" + " Parked at:" + convert(currTime); 
} 

Attandant:

@Override 
public String toString() { 
    return "Attendant [staffNum=" + staffNum + ", id=" + id + ", available=" + available + ", name=" + name 
      + ", Car=" + assign + "]"; 
} 

PS:英語不是我的母語。

我的整個代碼是在這裏:

public class Cars { 

    private static String carID; 
    private String plateNum; 
    private static String position; 
    private static 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; 
     Cars.position = position; 
     Cars.assignedTo = assignedTo; 
     this.currTime = currTime; 
    } 

    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 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 static 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) { 
     ... 
    } 

    @Override 
    public String toString() { 
     return "Car:" + plateNum + " ID:" + carID + " Position:" + position + " Assigned to:" + assignedTo.getId() 
     + "(" + assignedTo.getName() + ")" + " Parked at:" + convert(currTime); 
    } 
} 

public class Attendant { 

    private static int staffAmount = 10; 

    public int staffNum; 
    private String id; 
    private boolean available; 
    private attNm name; 
    private Cars assign; 

    private String user; 
    static String[] tempArray = new String[staffAmount]; 
    static ArrayList<Attendant> myAtt = new ArrayList<Attendant>(); 

    public Attendant(int staffNum, String id, boolean available, attNm name, Cars assign) { 
     this.staffNum = staffNum; 
     this.id = id; 
     this.available = available; 
     this.name = name; 
     this.assign = assign; 
    } 

    public Attendant(String user) { 
     this.user = user; 
    } 

    public static void createAtt() { 
     createIDList(); 
     int staffAmount = getStaffAmount(); 

     Attendant att = null; 
     for (int x = 0; x < staffAmount; x++) { 
      att = new Attendant(x + 1, Attendant.tempArray[x], true, attNm.getNm(), null); 
      myAtt.add(att); 
     } 

     for (int x = 0; x < myAtt.size(); x++) { 
      System.out.println(myAtt.get(x)); 
     } 
    } 

    public static void startWork() { 
     createAtt(); 
    } 

    public static Attendant askForAtt() { 
     Scanner scanAtt = new Scanner(System.in); 
     Random randAtt = new Random(); 
     //Attendant asnAtt = null; 
     System.out.println("Do you require an Attendant ? Y or N"); 
     String response = scanAtt.next(); 
     if ((response.equals("y")) || (response.equals("yes")) || (response.equals("Yes")) || (response.equals("Y"))) { 
      // Cars.setAssignedTo(myAtt.get(randAtt.nextInt(myAtt.size()))); 
      Attendant attendant = myAtt.get(randAtt.nextInt(myAtt.size())); 
      while(!attendant.isAvailable()){ 
       attendant = myAtt.get(randAtt.nextInt(myAtt.size())); 
      } 
      return attendant; 

     } else if ((response.equals("n")) || (response.equals("no")) || (response.equals("No")) || (response.equals("N"))) { 
      return new Attendant ("User"); 
     } 
     return new Attendant ("User"); //If input is neither Yes nor No then return new Attendant  
    } 

    public static void assignCarAtt(){ 

    } 

    public int getStaffNum() { 
     return staffNum; 
    } 

    public static int getStaffAmount() { 
     return staffAmount; 
    } 

    public void setStaffNum(int staffNum) { 
     this.staffNum = staffNum; 
    } 

    public void setAvailable(boolean available) { 
     this.available = available; 
    } 

    public boolean getAvailable(){ 
     return available; 
    } 

    public void setAssign(Cars assign) { 
     this.assign = assign; 
    } 

    public String getId() { 
     return id; 
    } 

    public boolean isAvailable() { 
     return available; 
    } 

    public attNm getName() { 
     return name; 
    } 

    public static void createIDList() { 
     for (int x = 0; x < staffAmount; x++) { 
      tempArray[x] = ("Att" + (x + 1)); 
     } 
    } 

    public String strAssignAttCar(Cars assign){ 
     String result = null; 
     if(assign!=null){ 
      result = Cars.getCarID(); 
     } 
     return result; 
    } 

    @Override 
    public String toString() { 
     return "Attendant [staffNum=" + staffNum + ", id=" + id + ", available=" + available + ", name=" + name 
       + ", Car=" + assign + "]"; 
    } 

} 

車庫類打印;

Car:HU 457 ID:CR2 Position:GR2 Assigned to:Att10(Ava) Parked at:18:32:08, Car:HU 457 ID:CR2 Position:GR2 Assigned to:Att9(Elizabeth) Parked at:18:32:21] 
    // Should have been like that: 
    Car:HU 457 ID:CR1 Position:GR1 Assigned to:Att10(Ava) Parked at:18:32:08, Car:HU 457 ID:CR2 Position:GR2 Assigned to:Att9(Elizabeth) Parked at:18:32:21] 

服務員等級打印;

Attendant [staffNum=9, id=Att9, available=false, name=Elizabeth, Car=Car:HU 457 ID:CR2 Position:GR2 Assigned to:Att9(Elizabeth) Parked at:18:32:21] 
    Attendant [staffNum=10, id=Att10, available=false, name=Ava, Car=Car:HU 457 ID:CR2 Position:GR2 Assigned to:Att10(Ava) Parked at:18:32:08] 
    // Should have been like that: 
    Attendant [staffNum=9, id=Att9, available=false, name=Elizabeth, Car=Car:HU 457 ID:CR2 Position:GR2 Assigned to:Att9(Elizabeth) Parked at:18:32:21] 
    Attendant [staffNum=10, id=Att10, available=false, name=Ava, Car=Car:HU 457 ID:CR1 Position:GR1 Assigned to:Att10(Ava) Parked at:18:32:08] 

回答

2

首先,它更有意義,使Cars類奇異,即Car

背景信息:在聲明中Cars car1 = new Cars()Cars是類,並car1Cars類的一個實例。這個問題是由兩個混淆造成的。

靜態變量/方法不與任何特定的類實例相關聯;它只與班級有關。因此,當您聲明carIDstatic時,您說只會有一個全局的Cars.carID,而不是多個car1.carID,car2.carID等。確保實例變量/方法不是靜態的。

當您刪除static關鍵字時所遇到的問題是因爲您致電Cars.getCarID()而不是car1.getCarID()。您需要將該類上的所有方法調用更改爲該類實例上的方法調用。

tl; dr:要解決此問題,請刪除所有static關鍵字並將所有靜態引用更改爲諸如Cars.getCarID()car1.getCarID()之類的方法。面向對象的編程的

實施例(注意沒有靜態方法或變量,除了主要方法):

Car.java:

class Car { 
    int id; 
    int speed; 

    Car(int id, int speed) { 
     this.id = id; 
     this.speed = speed; 
    } 

    int getSpeed() { 
     return speed; 
    } 

    void setSpeed(int newSpeed) { 
     speed = newSpeed; 
    } 

    int getId() { 
     return id; 
    } 

    void goFast() { 
     System.out.println("VROOM! This car is going so fast: " + speed); 
    } 
} 

Person.java:

class Person { 
    Car myCar; 
    int id; 

    Person(int id, Car car) { 
     this.id = id; 
     this.myCar = car; 
    } 

    int getCar() { 
     return myCar; 
    } 

    int getId() { 
     return id; 
    } 

    void floorIt() { 
     myCar.setSpeed(myCar.getSpeed() + 10); 

    void drive() { 
     myCar.goFast(); 
    } 
} 

Main.java:

class Main { 
    public static void main(String[] args) { 
     Car ferrari = new Car(1, 1000); 
     Car bmw = new Car(2, 500); 

     Person alice = new Person(1, ferrari); 
     Person bobby = new Person(2, bmw); 

     alice.drive(); 
     bobby.drive(); 

     bobby.floorIt(); 
     bobby.drive(); 

    } 
} 
+0

When我做類似這樣的事情:Cars car1 = new Cars(car1.getID(),car1.askCarID().......);它告訴我本地變量car1可能沒有初始化 – Kavi

+0

更多建議plz? – Kavi

+0

@Kavi,你不能在car1的構造函數上調用方法。它還不存在,所以它沒有ID –