AnyLogic中Event块无法识别ExportInfo类与list变量问题求助
问题解决步骤
错误原因分析
- 类作用域限制:你在Wait块内定义的
ExportInfo是局部内部类,仅能在Wait块的代码中使用,Event块无法识别该类,因此报错“ExportInfo cannot be resolved to a type”。且局部类不允许加public修饰符,这也是你加public后报错的原因。 - 变量作用域限制:Wait块内的
LinkedList<ExportInfo>是局部变量,同样无法被Event块访问,导致“list cannot be resolved to a variable”错误。
具体修复方案
1. 将ExportInfo类移至全局作用域
在AnyLogic项目树的Classes节点右键,选择「New Class」,命名为ExportInfo,替换为以下修正后的代码:
public class ExportInfo { public int id; public int P; public double d; // 修正构造方法:使用传入参数赋值(与原调用逻辑匹配) public ExportInfo(int id, int P, double d) { this.id = id; this.P = P; this.d = d; } // 可选:更简洁的构造方式(直接接收Agent对象,避免重复传参) // public ExportInfo(Agent agent) { // this.id = agent.atrID; // this.P = agent.atrEstimatedPackingTime; // this.d = dateToTime(agent.atrDueDate); // } }
注:原构造方法未使用传入的参数,直接硬写
agent.xxx是错误的,需修正为用参数赋值或直接接收Agent对象。
2. 定义全局链表变量
在模型的Main节点右键选择「Add Variable」:
- 类型:
LinkedList<ExportInfo> - 变量名:
exportList - 初始值:
new LinkedList<>()
3. 修改Wait块代码
删除原Wait块内的类和局部链表定义,改为向全局链表添加元素:
// 对应构造方法方案1 exportList.add(new ExportInfo(agent.atrID, agent.atrEstimatedPackingTime, dateToTime(agent.atrDueDate))); // 若用构造方法方案2,改为: // exportList.add(new ExportInfo(agent));
4. 修改Event块代码
将原代码中的list替换为全局的exportList:
int row = 1; for(ExportInfo e : exportList){ ALtoGA.setCellValue(e.id, 1, row, 1); ALtoGA.setCellValue(e.P, 1, row, 2); ALtoGA.setCellValue(e.d, 1, row, 3); row++; } ALtoGA.writeFile(); // 可选:若需避免重复写入,写完后清空链表 // exportList.clear();
内容的提问来源于stack exchange,提问作者Marloes
相关产品推荐
相关产品推荐

