题目描述
下面程序定义了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)
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")
这是基础题你网上搜一下,都有,我晚上回来看看