gpt4 book ai didi

java - 将对象作为参数传递给其他对象 - null

转载 作者:行者123 更新时间:2023-12-02 00:10:08 26 4
gpt4 key购买 nike

我正在构建一个铁路模拟器,其中包含类模拟器、车站、火车、路线和乘客。我在将路线对象作为参数传递给火车对象时遇到问题 - 它始终显示为空。我已将我的代码作为解释我的问题的最佳方式。

import java.util.ArrayList;

public class Simulator {
int numStations;
Route route1;
ArrayList<Station> stations;
ArrayList<Train> trains;
Route[] routes;

public static void main(String[] args) {

//set up network
Simulator sim = new Simulator();
ArrayList<Station> stations = new ArrayList<Station>();
stations = sim.generateStations();
Route[] routes;
routes = sim.generateRoutes(stations);
System.out.println("Route array " + routes);
ArrayList<Train> trains = new ArrayList<Train>();
trains = sim.generateTrains(routes);

//start simulator
sim.generatePassengers(stations);
Train currentTrain = trains.get(0);
System.out.println("current train " + currentTrain);
//route null here
System.out.println("Route of current train " + currentTrain.route);


}


public Route[] generateRoutes(ArrayList<Station> stations){
//initialise routes between stations

Station[] stationList1 = {stations.get(0), stations.get(1), stations.get(2), stations.get(3), stations.get(4), stations.get(5)};
int[] stationDist1 = {200,100,200,300,200,300};
Route route1 = new Route(true, stationList1, stationDist1);
System.out.println("route1 " + route1);

//make list of all routes
Route[] routeList;
routeList = new Route[1];
routeList[0] = route1;

return routeList;
}

public ArrayList<Train> generateTrains(Route[] routes){
//initialise trains
ArrayList<Train> trainList = new ArrayList<Train>();
trainList.add(new Train(routes[0], 100, 0, true, 5));
trainList.add(new Train(routes[0], 100, 4, false, 10));

System.out.println("first train in list " + trainList.get(0));
System.out.println("first route in array " + routes[0]);
//route is null here
System.out.println("route of first train " + (trainList.get(0)).route);

return trainList;
}
}

这是我的输出:

route1 Route@addbf1

Route array [LRoute;@42e816

Route1 in array Route@addbf1

first train in list Train@190d11

first route in array Route@addbf1

route of first train null

current train Train@190d11

Route of current train null

谁能解释一下我哪里出错了?

编辑:列车等级声明:

public class Train {
Route route;
int capacity;
int recentLoc;
boolean forwards;
boolean atStation;
int speed;
Train currentTrain;
int timeAtStation;

//train constructor
public Train(Route r, int c, int i, boolean f, int s){
r = route;
c = capacity;
i = recentLoc;
f = forwards;
s = speed;
atStation = true;
timeAtStation = 0;
}

最佳答案

您没有为实例变量赋值。对象的默认值为 null。在 Train 构造函数中将“r = route”更改为“route = r”(所有其他字段相同)。

public class Train {
Route route;
int capacity;
int recentLoc;
boolean forwards;
boolean atStation;
int speed;
Train currentTrain;
int timeAtStation;

//train constructor
public Train(Route r, int c, int i, boolean f, int s){
route = r;
capacity = c;
recentLoc = i;
forwards = f;
speed = s;
atStation = true;
timeAtStation = 0;
}

关于java - 将对象作为参数传递给其他对象 - null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13027558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com