You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何集成Scanner实现继承类的自定义属性输入

如何通过Scanner实现学校属性的用户输入?

我来帮你搞定这个需求!咱们可以通过Scanner一步步引导用户输入所需的属性值,然后实例化对应的学校类。先给你修正原代码里的小问题(比如BandA构造方法的括号没闭合),再给出完整的实现方案:

完整修改后的代码

import java.util.Scanner;

public class PrimarySchools {
    String address;

    public PrimarySchools(String schoolAddress) {
        address = schoolAddress;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String schoolAddress) {
        this.address = schoolAddress;
    }
}

class BandA extends PrimarySchools {
    String name;
    String type;
    String opening;

    // 修正了原代码里缺失的闭合括号
    public BandA(String schoolName, String schoolType, String openingHours, String schoolAddress) {
        super(schoolAddress);
        this.name = schoolName;
        this.type = schoolType;
        this.opening = openingHours;
    }

    // 添加打印信息方法,方便验证输入结果
    public void showSchoolInfo() {
        System.out.println("A类学校信息:");
        System.out.println("学校名称:" + name);
        System.out.println("学校类型:" + type);
        System.out.println("开放时间:" + opening);
        System.out.println("学校地址:" + getAddress());
    }
}

class BandB extends PrimarySchools {
    String name;
    String type;
    String opening;

    public BandB(String schoolName, String schoolType, String openingHours, String schoolAddress) {
        super(schoolAddress);
        this.name = schoolName;
        this.type = schoolType;
        this.opening = openingHours;
    }

    // 同样添加打印方法
    public void showSchoolInfo() {
        System.out.println("B类学校信息:");
        System.out.println("学校名称:" + name);
        System.out.println("学校类型:" + type);
        System.out.println("开放时间:" + opening);
        System.out.println("学校地址:" + getAddress());
    }
}

class Main {
    public static void main(String[] args) {
        Scanner schoolData = new Scanner(System.in);

        // 引导用户逐个输入属性值
        System.out.print("请输入学校名称:");
        String schoolName = schoolData.nextLine();

        System.out.print("请输入学校类型:");
        String schoolType = schoolData.nextLine();

        System.out.print("请输入学校开放时间:");
        String openingHours = schoolData.nextLine();

        System.out.print("请输入学校地址:");
        String schoolAddress = schoolData.nextLine();

        // 让用户选择要创建的学校类别
        System.out.print("请选择学校类别(输入A或B):");
        String bandChoice = schoolData.nextLine();

        // 根据选择实例化对应学校对象并展示信息
        if (bandChoice.equalsIgnoreCase("A")) {
            BandA bandASchool = new BandA(schoolName, schoolType, openingHours, schoolAddress);
            bandASchool.showSchoolInfo();
        } else if (bandChoice.equalsIgnoreCase("B")) {
            BandB bandBSchool = new BandB(schoolName, schoolType, openingHours, schoolAddress);
            bandBSchool.showSchoolInfo();
        } else {
            System.out.println("输入的类别无效!");
        }

        schoolData.close();
    }
}

关键实现说明

  • 先修正了原代码中BandA构造方法缺失闭合括号的语法错误
  • 在BandA和BandB中新增showSchoolInfo()方法,用来验证用户输入的内容是否正确赋值给对象属性
  • 在Main类的main方法里:
    1. 通过Scanner.nextLine()方法读取用户输入的每个属性值
    2. 增加学校类别选择逻辑,让用户自主选择创建A类或B类学校
    3. 根据用户选择实例化对应对象,并调用打印方法展示完整学校信息
    4. 最后关闭Scanner资源,避免内存泄漏

如果需要批量创建多所学校,还可以把输入逻辑封装成独立方法,循环调用后将实例化的对象存入PrimarySchools类型的数组/集合中,替代原代码里的String[] maxSchools,这样更贴合业务逻辑。

内容的提问来源于stack exchange,提问作者user13322583

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 07:37:41