学生系统 问题描述 设计一个学生类 Student,其属性有 name(姓名)、age(年龄)和 degree(学位)。由 Student 类派生出本科生类 Undergraduate 和研究生类 Graduate,Undergraduate 类增加属性 specialty(专业),研究生类增加属性 direction(研究方向)。每个类都有 show()方法, 用于输出属性信息。 注: (1) 所有的输入均为英文,不含中文。 (2) 编写测试类 Test 进行测试,分别声明 2 个类的对象,并调用 show()方法。 (3) 输出结果为: name:sky age:23 degree:Graduate direction:software name:sky age:23 degree:Undergraduate specialty:java 要Java代码
时间: 2024-03-18 20:38:54 浏览: 134
设计一个学生类Student和它的一个子类Undergraduate,要求如下
以下是Java代码实现:
```java
class Student {
String name;
int age;
String degree;
public void show() {
System.out.println("name:" + name + " age:" + age + " degree:" + degree);
}
}
class Undergraduate extends Student {
String specialty;
public void show() {
System.out.println("name:" + name + " age:" + age + " degree:" + degree + " specialty:" + specialty);
}
}
class Graduate extends Student {
String direction;
public void show() {
System.out.println("name:" + name + " age:" + age + " degree:" + degree + " direction:" + direction);
}
}
public class Test {
public static void main(String[] args) {
Undergraduate u = new Undergraduate();
u.name = "sky";
u.age = 23;
u.degree = "Undergraduate";
u.specialty = "java";
u.show();
Graduate g = new Graduate();
g.name = "sky";
g.age = 23;
g.degree = "Graduate";
g.direction = "software";
g.show();
}
}
```
输出结果为:
```
name:sky age:23 degree:Undergraduate specialty:java
name:sky age:23 degree:Graduate direction:software
```
阅读全文