我需要關於這段代碼的幫助。在對象數組中訪問object.variable
public class ParkingLot {
static int MAX = 5;
static Car[] Slot = new Car[MAX];
public static void main(String[] args) {
Slot[0] = new Car("1234", "White");
Slot[1] = new Car("5678", "Black");
}
public static void Allot() {
for (int i = 0; i <= Slot.length; i++) {
System.out.println(Slot.getNo);
}
}
我在Slot
存儲Car
對象。我希望打印/訪問存儲在插槽中的汽車的No
和Colour
。我該如何去做呢?
嗯,(汽車:插槽)是如何工作的? – theTuxRacer 2010-06-26 07:55:47
它是「語法糖」 - 循環遍歷'slot'數組/集合的所有元素。通常,'slot'應該實現'Iterable',但是對於數組也是如此。 – Bozho 2010-06-26 07:59:28
另外,方法名稱也應該小寫。 「Allot()」方法應該重命名爲「allot()」 – Michael 2010-06-26 14:35:30