求解下面的Java填空题,//的地方是需要填空的。谢谢
protected String name;
protected char sex;
//
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;
}
}
public class Student extends Person{
protected String id;
Student(String name,char sex){
//
}
Student(String name,char sex,String id){
super(name,sex);
this.id = id;
}
public String toString() {
String s = new String(name + "(sex:" + sex);
//
s += ")";
return s;
}
//
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);
//
System.out.println("tom: " + tom);
}
} 展开
//Person
package test20201223;
public class Person {
protected String name;
protected char sex;
//这里应该是一个构造方法,或者set方法,
//构造方法
public Person(){}
public Person(String name,char sex){
this.name = name;
this.sex = sex;
}
//set方法
public void setPerson(String name,char sex){
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;
}
}
//Student
package test20201223;
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);
//改变tom属性
tom.setPerson("tom", 'M');
System.out.println("tom: " + tom);
}
}
//Main
package test20201223;
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);
//改变tom属性
tom.setPerson("tom", 'M');
System.out.println("tom: " + tom);
}
}