AnyLogic访问Sedan代理queueEntryTime报错:无法静态引用非静态字段
解决方法
错误原因很明确:你用Sedan.queueEntryTime是通过类名访问非静态成员,而queueEntryTime是每个Sedan实例独有的属性,不属于类本身,Java不允许这种静态引用方式。
按以下步骤修改代码:
获取当前通过停止线的具体Sedan实例
在停止线的触发代码中,用AnyLogic提供的getVehicle()方法拿到当前车辆,再强转为Sedan类型:Sedan currentSedan = (Sedan) getVehicle();计算并统计等待时间
通过获取到的实例访问queueEntryTime,完成计算和统计:double waitingTime = time() - currentSedan.queueEntryTime; vehicleWaitingTimeStats.add(waitingTime);可选:兼容多车辆类型
如果你的模型里还有其他类型车辆,加个类型判断避免强转报错:Vehicle vehicle = getVehicle(); if (vehicle instanceof Sedan) { Sedan currentSedan = (Sedan) vehicle; double waitingTime = time() - currentSedan.queueEntryTime; vehicleWaitingTimeStats.add(waitingTime); }
额外提醒:确保queueEntryTime是在车辆进入队列时被正确赋值为time(),比如在车辆接近停止线的触发逻辑里设置,否则统计的等待时间会不准确。
内容的提问来源于stack exchange,提问作者Fair
相关产品推荐
相关产品推荐

