0
現在我有3個類。火車,旅行車和乘客。在火車類使用來自其他類的輸入
public class Train
{
private int seatNum = 30 + (int) (Math.random() * ((40 - 30) + 1)); //Random number of seats
private int wagonNum = 5 + (int) (Math.random() * ((10 - 5) + 1));//Random number of wagons
public Train()
{
PassWagon[] wagons = new PassWagon[wagonNum];//here I created an array with random wagon numbers.
for (int i = 0; i < wagonNum; i++)
{
wagons[i] = new PassWagon(seatNum); // I added PassWagon objects to that array. I need to access "seatnum" in the PassWagon class.
}
}
}
和
public class PassWagon
{
private int wagonseats ;
Passenger[] rowA = new Passenger[wagonseats]; //I created 2 "Passenger" arrays. Problem is, wagonseats variable has nothing. "seatNum" variable from other class gets initiated at Constructor.
Passenger[] rowB = new Passenger[wagonseats];
public PassWagon(int seat)
{
this.wagonseats = seat;
for (int i = 0; i < seats; i++)
{
rowA[i] = new Passenger(); //I get an out of bounds error here because this array has nothing in it.
rowB[i] = new Passenger();
}
}
我想要得到的 「seatNum」 變量並使用它來創建行rowA和rowB中的陣列。我嘗試過幾件事:
- 我在PassWagon類中生成了隨機數,但是這次每個貨車對象都有不同數量的座位。我希望所有的貨車都有相同數量的座位。
我在構造函數裏面創建了rowA和rowB數組,這次我無法在構造函數外聲明rowA和rowB數組。
有沒有人有任何想法?