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数组。
有没有人有任何想法?