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

2024-10-30 10:27:01
推荐回答(1个)
回答1:

class Student {
public String name;
public int age;
public Student(String name,int age){
this.name=name;
this.age=age;
}
public void show(){
System.out.println("name: "+name+" age: "+age);
}
}
class UnderGraduate extends Student{
public String degree;
public UnderGraduate(String name,int age,String degree){
super(name, age);
this.degree=degree;
}
public void show(){
System.out.println("name: "+name+" age: "+age+" degree: "+degree);
}
}
public class Test01{
public static void main(String[] args) {
Student student = new Student("zhangsan", 16);
student.show();
UnderGraduate underGraduate = new UnderGraduate("lisi", 20, "bechalor");
underGraduate.show();
}
}