Java继承代码循环异常求助:选择1无法调用Agent子类
问题定位:Java父子类构造器引发的无限循环
我编写了包含Person父类、Agent和Customer子类的Java代码,运行时出现异常循环:未编写循环逻辑,但输入1尝试调用Agent子类时,程序陷入重复显示[1]AGENT/[2]CUSTOMER选项的死循环;输入2则可正常执行对应逻辑。已经排查5-7小时,实在找不到问题所在。
相关代码
Person父类代码
import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; import java.util.ArrayList; import java.util.List; class Person { protected String agentId = "20860132"; protected String password = "20020729" ; protected String address; public Person(String agentId,String password, String address) { this.agentId = agentId; this.password = password; this.address = address; Scanner input = new Scanner(System.in); System.out.println("[1]AGENT"); System.out.println("[2]CUSTOMER"); int choice = input.nextInt(); // 问题出在这里:程序会反复输出上面两行选项 if(choice == 1) { Agent agent = new Agent("Niel", "diko alam", "umay"); } else if(choice == 2) { System.out.println("POTANGINA"); } } }
Agent和Customer子类代码
class Agent extends Person { public Agent(String agentId, String password, String address) { super(agentId, password, address); Scanner input2 = new Scanner(System.in); System.out.println("[LOGIN]"); System.out.print("ENTER AGENT ID:"); int id = input2.nextInt(); System.out.print("ENTER PASSWORD:"); int pass = input2.nextInt(); if(id == 20860132 && pass == 20020729) { Scanner input = new Scanner(System.in); System.out.println("[1]ADD CAR"); System.out.println("[2]SCHEDULE"); System.out.println("[3]RECORDS"); int choice2 = input.nextInt(); if(choice2 == 1) { boolean stopFlag = false; do { List<String>cars = new ArrayList<String>(); System.out.println("[CARS]"); cars.add("Tayota"); cars.add("Hillux"); cars.add("Bugatti"); System.out.println(cars); System.out.println("Enter Car:"); String car = input.nextLine(); cars.add(car); System.out.println("Would you like to add more?"); System.out.println("[1]YES"); System.out.println("[2]NO"); String choice3 = input.nextLine(); addCar(cars); if(!choice3.equals(1)) stopFlag = true; }while(!stopFlag); } } else { System.out.println("INCORRECT PLEASE TRY AGAIN."); } } public void addCar(List<String> cars) { try { FileWriter fw = new FileWriter("cars.txt", true); PrintWriter pw = new PrintWriter(fw); pw.println(cars); pw.close(); } catch (IOException e) { e.printStackTrace(); } } public void schedule(String schedule) { try { FileWriter fw = new FileWriter("schedule.txt", true); PrintWriter pw = new PrintWriter(fw); pw.println(schedule); pw.close(); } catch (IOException e) { e.printStackTrace(); } } public void records(String record) { try { FileWriter fw = new FileWriter("records.txt", true); PrintWriter pw = new PrintWriter(fw); pw.println(record); pw.close(); } catch (IOException e) { e.printStackTrace(); } } } class Customer extends Person { private String customerId; public Customer(String agentId, String password, String address, String customerId) { super(agentId, password, address); this.customerId = customerId; } public void setCustomerId(String customerId) { this.customerId = customerId; } public String getCustomerId() { return customerId; } public void rentCar(String car) { try { FileWriter fw = new FileWriter("cars.txt", true); PrintWriter pw = new PrintWriter(fw); pw.println(car); pw.close(); } catch (IOException e) { e.printStackTrace(); } } public void viewSchedule(String schedule) { try { FileWriter fw = new FileWriter("schedule.txt", true); PrintWriter pw = new PrintWriter(fw); pw.println(schedule); pw.close(); } catch (IOException e) { e.printStackTrace(); } } public void extend(String record) { try { FileWriter fw = new FileWriter("records.txt", true); PrintWriter pw = new PrintWriter(fw); pw.println(record); pw.close(); } catch (IOException e) { e.printStackTrace(); } } }
主方法代码
public class Finals { public static void main(String[] args) { Person person = new Person("20860132", "h208f32", "San luis"); Agent agent = new Agent("20860132", "h208f32", "San luis"); } }
问题根源分析
无限递归循环的核心原因:
Person构造器中执行new Agent(...)创建子类实例,但Agent继承自Person,创建Agent时必须先调用父类Person的构造器(super())。这就形成了死循环:- 进入Person构造器 → 输入1 → 创建Agent → 调用Person构造器 → 再次显示选项 → 输入1 → 创建Agent → 无限重复此流程
次要问题:
- 主方法同时创建Person和Agent实例,导致程序启动就执行两次Person构造器的选择逻辑
- Agent类中
choice3.equals(1)是字符串与整数直接比较,永远返回false,会导致添加车辆的循环无法停止 - 重复创建Scanner实例,容易引发输入缓冲异常
修复方案
1. 剥离构造器中的业务逻辑
构造器仅负责初始化属性,把用户选择逻辑从Person构造器中移除:
class Person { protected String agentId = "20860132"; protected String password = "20020729" ; protected String address; public Person(String agentId,String password, String address) { this.agentId = agentId; this.password = password; this.address = address; } }
2. 把选择逻辑移到主方法中
由主方法负责接收用户输入,创建对应实例:
public class Finals { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("[1]AGENT"); System.out.println("[2]CUSTOMER"); int choice = input.nextInt(); if(choice == 1) { Agent agent = new Agent("20860132", "h208f32", "San luis"); } else if(choice == 2) { System.out.println("POTANGINA"); // 此处可添加Customer实例创建逻辑 // Customer customer = new Customer("xxx", "xxx", "xxx", "xxx"); } input.close(); } }
3. 修复Agent类中的字符串比较问题
将字符串与整数的错误比较改为字符串间的比较,同时处理输入缓冲的换行符:
// 修改Agent类中添加车辆的循环部分 if(choice2 == 1) { boolean stopFlag = false; do { List<String>cars = new ArrayList<String>(); System.out.println("[CARS]"); cars.add("Tayota"); cars.add("Hillux"); cars.add("Bugatti"); System.out.println(cars); System.out.println("Enter Car:"); // 处理上一次nextInt()遗留的换行符 input.nextLine(); String car = input.nextLine(); cars.add(car); System.out.println("Would you like to add more?"); System.out.println("[1]YES"); System.out.println("[2]NO"); String choice3 = input.nextLine(); addCar(cars); // 改为字符串比较 if(choice3.equals("2")) stopFlag = true; }while(!stopFlag); }
4. 统一使用单个Scanner实例
避免重复创建Scanner,减少输入异常风险。
内容的提问来源于stack exchange,提问作者newbie
相关产品推荐
相关产品推荐

