java实现学生的属性:姓名、性别、年龄、学号、年级、专业;学生的行为:吃饭、娱乐、学习、写作业。教师的属性:姓名、性别、年龄、工号、职称、部门;教师的行为:吃饭、娱乐、授课、改作业。请设计出对应的继承体系,并在主类中完成测试。
时间: 2023-10-02 22:10:21 浏览: 96
以下是Java实现学生和教师的继承体系代码:
```
// 人类
class Person {
String name;
String gender;
int age;
void eat() {
System.out.println(name + " is eating.");
}
void play() {
System.out.println(name + " is playing.");
}
}
// 学生类
class Student extends Person {
String id;
String grade;
String major;
void study() {
System.out.println(name + " is studying.");
}
void doHomework() {
System.out.println(name + " is doing homework.");
}
}
// 教师类
class Teacher extends Person {
String id;
String title;
String department;
void teach() {
System.out.println(name + " is teaching.");
}
void gradeHomework() {
System.out.println(name + " is grading homework.");
}
}
```
以下是主类中的测试代码:
```
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.name = "Tom";
student.gender = "Male";
student.age = 18;
student.id = "20210001";
student.grade = "Freshman";
student.major = "Computer Science";
student.eat();
student.play();
student.study();
student.doHomework();
Teacher teacher = new Teacher();
teacher.name = "Mary";
teacher.gender = "Female";
teacher.age = 35;
teacher.id = "1001";
teacher.title = "Associate Professor";
teacher.department = "Computer Science";
teacher.eat();
teacher.play();
teacher.teach();
teacher.gradeHomework();
}
}
```
输出结果:
```
Tom is eating.
Tom is playing.
Tom is studying.
Tom is doing homework.
Mary is eating.
Mary is playing.
Mary is teaching.
Mary is grading homework.
```
以上就是Java实现学生和教师的继承体系的代码和测试。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)