Ark_Scorpion 2021-11-13 11:02 采纳率: 55.2%
浏览 87
已结题

下面程序定义了Student类和Person类,并在Main类中对它们进行了演示。演示结果如样例输出所示。请将程序填写完整。

题目描述
下面程序定义了Student类和Person类,并在Main类中对它们进行了演示。演示结果如样例输出所示。请将程序填写完整。

class Person {

    protected String name;
    protected char sex;

    【1】 {//空1
        this.name = name;
        this.sex = sex;
    }

    String name() {
        return name;
    }

    char sex() {
        return sex;
    }

    public String toString() {
        String s = new String(name + "(sex: " + sex + ")");
        return s;
    }

}

class Student extends Person {
    protected String id;

    Student(String name, char sex) {
        【2】;// 空2
    }

    Student(String name, char sex, String id) {
        super(name, sex);
        this.id = id;
    }

    public String toString() {
        String s = new String(name + "(sex:" + sex);
        【3//空3
        s += ")";
        return s;
    }

    【4】 {//空4
        this.id = id;
    }    

}

public class Main {

    public static void main(String[] args) {
        Person frank = new Person("Frank", 'M');
        Student alice = new Student("Alice", 'F');
        System.out.println("frank: " + frank);
        System.out.println("alice: " + alice);
        Person tom = alice;
        System.out.println("tom: " + tom);
        【5】;// 空5
        System.out.println("tom: " + tom);
    }

}

输出样例
frank: Frank(sex: M)
alice: Alice(sex:F)
tom: Alice(sex:F)
tom: Alice(sex:F;id:00000001)

  • 写回答

2条回答 默认 最新

  • chencc98 2021-11-13 12:35
    关注
    
    1: public Person(String name, char sex)
    
    2: super(name, sex)
    
    3:
    if (this.id != null) {
      s += ";id=" + this.id;
    }
    
    4: public void setId(String id)
    
    5: alice.setId("00000001")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月21日
  • 已采纳回答 11月13日
  • 修改了问题 11月13日
  • 创建了问题 11月13日