java 设计一个学生类Student和它的一个字类Undergraduate,要求如下:

2024-12-25 19:23:30
推荐回答(3个)
回答1:

这么简单啊。
public class Student {
private String name;
private int age;

public Student() {

}

public Student(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public void show() {
System.out.println(String.format("name=%s \tage=%s ", name, age));

}

}
public class Undergraduate extends Student {
private String degree;

public Undergraduate() {

}

public Undergraduate(String degree, String name, int age) {
super(name, age);
this.degree = degree;
}

@Override
public void show() {

System.out.println(String.format("degree=%s \tname=%s \tage=%s",
degree, super.getName(), super.getAge()));

}

}
public class StuTest {

public static void main(String[] args) {

//基类
Student stu = new Student("李四",22);
stu.show();
//子类
Student degree = new Undergraduate("大学",stu.getName(),stu.getAge());
degree.show();

}

}

回答2:

可以写的 写就see my name, find me

回答3:

老师,我不会啦!!我是来打酱油额!!