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

Java灾害管理系统实验中User类构造器不匹配教程代码报错问题

问题根因

你当前编写的User类没有提供教程要求的4参数紧急联系人构造器,现有仅支持2种构造器签名:

  • 7参数构造器:User(String firstName, String lastName, boolean gender, int age, String blood, String telephone, String email)
  • 6参数构造器:User(String firstName, String lastName, boolean gender, int age, String telephone, String email)

教程给出的4参数调用new User("Marlena", "Evans","6088861222", "me@daysofourlives.com"),参数顺序是[名、姓、电话、邮箱],和你现有构造器的参数数量、类型都不匹配,所以编译器会抛出参数不匹配的报错。

为什么修改后的代码能运行

你调整后的6参数调用,刚好匹配你最后定义的6参数构造器的签名,参数数量、类型一一对应,所以编译报错消失。

注意:你当前的6参数构造器存在隐藏bug,只赋值了email,漏掉了telephone的赋值,后续调用getTelephone()获取紧急联系人电话时会返回null,需要补上对应赋值逻辑:

public User (
        String firstName,
        String lastName,
        boolean gender,
        int age,
        String telephone,
        String email
){
    this.firstName = firstName;
    this.lastName = lastName;
    this.gender = gender;
    this.age = age;
    this.telephone = telephone; // 补充该行
    this.email = email;
}

若要符合教程的写法要求,补充4参数构造器即可

在User类中新增教程要求的紧急联系人专用构造器,其他字段可设置默认值:

// 紧急联系人构造器
public User (
        String firstName,
        String lastName,
        String telephone,
        String email
){
    this.firstName = firstName;
    this.lastName = lastName;
    this.telephone = telephone;
    this.email = email;
    // 非必填字段设置默认值
    this.gender = false;
    this.age = 0;
}

新增后即可直接使用教程给出的4参数代码创建u2实例。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 13:39:02