在一个学校中有教师和学生两类人员。学生信息有身份证号、学号、专业、姓名性别、年龄及数学、英语、Java程序设计三门课程的成绩。教师信息有身份证号、教师号、专业、姓名、性别、年龄及授课的学时数。创建学生和教师的对象并输出对象的信息。java代码,使用继承性
时间: 2023-09-12 12:05:58 浏览: 72
下面是使用继承性的Java代码实现:
```java
// 定义人员类Person
class Person {
private String idNumber; // 身份证号
private String major; // 专业
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
public Person(String idNumber, String major, String name, String gender, int age) {
this.idNumber = idNumber;
this.major = major;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getIdNumber() {
return idNumber;
}
public String getMajor() {
return major;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
}
// 定义学生类Student,继承自Person类
class Student extends Person {
private String studentNumber; // 学号
private int mathScore; // 数学成绩
private int englishScore; // 英语成绩
private int javaScore; // Java程序设计成绩
public Student(String idNumber, String major, String name, String gender, int age, String studentNumber, int mathScore, int englishScore, int javaScore) {
super(idNumber, major, name, gender, age);
this.studentNumber = studentNumber;
this.mathScore = mathScore;
this.englishScore = englishScore;
this.javaScore = javaScore;
}
public String getStudentNumber() {
return studentNumber;
}
public int getMathScore() {
return mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public int getJavaScore() {
return javaScore;
}
}
// 定义教师类Teacher,继承自Person类
class Teacher extends Person {
private String teacherNumber; // 教师号
private int teachingHours; // 授课的学时数
public Teacher(String idNumber, String major, String name, String gender, int age, String teacherNumber, int teachingHours) {
super(idNumber, major, name, gender, age);
this.teacherNumber = teacherNumber;
this.teachingHours = teachingHours;
}
public String getTeacherNumber() {
return teacherNumber;
}
public int getTeachingHours() {
return teachingHours;
}
}
// 测试
public class Test {
public static void main(String[] args) {
// 创建一个学生对象
Student student = new Student("123456199001011234", "计算机科学与技术", "张三", "男", 21, "20180001", 90, 85, 95);
System.out.println("学生信息:");
System.out.println("身份证号:" + student.getIdNumber());
System.out.println("专业:" + student.getMajor());
System.out.println("姓名:" + student.getName());
System.out.println("性别:" + student.getGender());
System.out.println("年龄:" + student.getAge());
System.out.println("学号:" + student.getStudentNumber());
System.out.println("数学成绩:" + student.getMathScore());
System.out.println("英语成绩:" + student.getEnglishScore());
System.out.println("Java程序设计成绩:" + student.getJavaScore());
// 创建一个教师对象
Teacher teacher = new Teacher("123456198001011234", "计算机科学与技术", "李四", "女", 41, "20010001", 48);
System.out.println("教师信息:");
System.out.println("身份证号:" + teacher.getIdNumber());
System.out.println("专业:" + teacher.getMajor());
System.out.println("姓名:" + teacher.getName());
System.out.println("性别:" + teacher.getGender());
System.out.println("年龄:" + teacher.getAge());
System.out.println("教师号:" + teacher.getTeacherNumber());
System.out.println("授课的学时数:" + teacher.getTeachingHours());
}
}
```
输出结果:
```
学生信息:
身份证号:123456199001011234
专业:计算机科学与技术
姓名:张三
性别:男
年龄:21
学号:20180001
数学成绩:90
英语成绩:85
Java程序设计成绩:95
教师信息:
身份证号:123456198001011234
专业:计算机科学与技术
姓名:李四
性别:女
年龄:41
教师号:20010001
授课的学时数:48
```
阅读全文