如何不使用static实现Java多类间vector同步与正确参数传递
解决方案
1 核心问题修正思路
你之前的问题本质是在消费数据的类中创建了全新的未初始化对象,正确方案是移除static修饰后,将已经完成数据加载的对象通过参数传递的方式注入到需要使用的类中,而非在类内部自行创建实例。
2 具体代码实现修改
第一步:重构ReadTemplate类,移除static修饰,封装数据
public class ReadTemplate { // 移除static,改为私有成员变量 private int[] templateVector = new int[13]; // 读文件方法,执行后templateVector就会被填充 public void readTemplate() throws NumberFormatException, IOException { File templatesFile= new File("C:\\Temp\\templatesFile.txt"); // 用try-with-resources自动关流,避免资源泄漏 try(FileReader fr = new FileReader(templatesFile); BufferedReader br = new BufferedReader(fr)) { String row; int j = 0; while((row= br.readLine()) != null) { String[] strings = row.split(" "); for (String str : strings) { Integer foo = Integer.parseInt(str); templateVector[j] = foo; j++; } } } } // 提供getter方法,不要直接暴露成员变量 public int[] getTemplateVector() { return templateVector; } }
第二步:封装投注实体类(适配「字符串名称+投注vector」结构)
// 单独定义投注条目实体 public class BetEntry { // 玩家/投注名称 private String betName; // 投注数据数组 private int[] vectorStakes; public BetEntry(String betName, int[] vectorStakes) { this.betName = betName; this.vectorStakes = vectorStakes; } public String getBetName() { return betName; } public int[] getVectorStakes() { return vectorStakes; } }
对应的ReadBets类读取完成后,返回List<BetEntry>类型的totalStakes即可,同样移除相关static修饰,提供getter方法返回加载完成的投注列表。
第三步:修改GenerateResults类,通过构造注入已初始化的对象
不要在类内部自行new ReadTemplate和ReadBets,改为从外部传入已经调用过读取方法、完成数据加载的实例:
public class GenerateResults { // 持有外部传入的已初始化对象 private ReadTemplate readTemplate; private ReadBets readBets; // 构造方法直接接收已加载完数据的实例 public GenerateResults(ReadTemplate readTemplate, ReadBets readBets) { this.readTemplate = readTemplate; this.readBets = readBets; } public void generateResult() throws IOException { File output = new File("C:\\Temp\\output.txt"); try(FileWriter fw = new FileWriter("c:\\Temp\\output.txt"); BufferedWriter bw = new BufferedWriter(fw)) { int[] templateVector = readTemplate.getTemplateVector(); List<BetEntry> totalStakes = readBets.getTotalStakes(); bw.write("TEMPLATE READ:"); for (int j = 0; j < templateVector.length; j++) { bw.write(templateVector[j] + " "); } bw.newLine(); bw.write("+--Result Bets:--+"); bw.newLine(); if (!output.exists()) { output.createNewFile(); } // 后续对比逻辑直接用上面拿到的templateVector和totalStakes即可 for(int j = 0; j < totalStakes.size(); j++) { int counter = 0; BetEntry currentBet = totalStakes.get(j); int[] betVector = currentBet.getVectorStakes(); for(int x = 0; x < templateVector.length; x++) { if (betVector[x] == templateVector[x]) { counter++; } } // 后续处理中奖逻辑按需补充 } } } }
第四步:统一调度初始化和调用流程
在程序入口统一执行初始化、加载数据、传参调用:
// 程序入口示例 public class Main { public static void main(String[] args) throws IOException { // 1 先初始化并加载模板数据 ReadTemplate readTemplate = new ReadTemplate(); readTemplate.readTemplate(); // 2 初始化并加载投注数据 ReadBets readBets = new ReadBets(); readBets.readBets(); // 替换为你实际的读取投注的方法名 // 3 将两个已加载完成的对象传入结果生成类 GenerateResults generateResults = new GenerateResults(readTemplate, readBets); // 4 执行结果生成 generateResults.generateResult(); } }
3 额外优化建议
- 优先使用依赖注入的方式传递已初始化的对象,避免滥用static全局变量,降低代码耦合度,也方便后续单元测试
- 所有IO流操作建议使用try-with-resources语法自动释放资源,不需要手动调用close(),避免异常场景下资源泄漏
- 类的成员变量尽量私有,通过getter/setter访问,不要直接暴露内部属性
内容的提问来源于stack exchange,提问作者Lekost93
相关产品推荐
相关产品推荐

