在一个学校中有教师和学生两类人员,学生信息有身份证号、学号、专业、姓名、性别年龄及数学、、英语和Java程序设计三门课程的成绩,教师信息有身份证号、教师号、专业、姓名、性别、年龄及授课的学时数,Java创建学生和教师的对象并输出对象的信息
时间: 2023-08-14 16:08:36 浏览: 112
好的,这是一个更加复杂的面向对象编程问题。以下是一个可能的实现,使用了 Java 语言:
```java
// 定义一个人员基类 Person
class Person {
String idCard;
String name;
int age;
String gender;
String major;
public Person(String idCard, String name, int age, String gender, String major) {
this.idCard = idCard;
this.name = name;
this.age = age;
this.gender = gender;
this.major = major;
}
public String toString() {
return String.format("身份证号:%s,姓名:%s,年龄:%d,性别:%s,专业:%s", idCard, name, age, gender, major);
}
}
// 定义一个学生类 Student,继承自 Person
class Student extends Person {
String studentId;
int mathScore;
int engScore;
int javaScore;
public Student(String idCard, String studentId, String name, int age, String gender, String major, int mathScore, int engScore, int javaScore) {
super(idCard, name, age, gender, major);
this.studentId = studentId;
this.mathScore = mathScore;
this.engScore = engScore;
this.javaScore = javaScore;
}
public String toString() {
return String.format("%s,学号:%s,数学成绩:%d,英语成绩:%d,Java程序设计成绩:%d", super.toString(), studentId, mathScore, engScore, javaScore);
}
}
// 定义一个教师类 Teacher,继承自 Person
class Teacher extends Person {
String teacherId;
int teachingHours;
public Teacher(String idCard, String teacherId, String name, int age, String gender, String major, int teachingHours) {
super(idCard, name, age, gender, major);
this.teacherId = teacherId;
this.teachingHours = teachingHours;
}
public String toString() {
return String.format("%s,教师号:%s,授课学时数:%d", super.toString(), teacherId, teachingHours);
}
}
// 创建一个学生对象和一个教师对象,并输出它们的信息
Student s = new Student("330121200001010001", "1001", "张三", 18, "男", "计算机科学与技术", 80, 90, 85);
Teacher t = new Teacher("330121197001010001", "2001", "李四", 30, "女", "计算机科学与技术", 20);
System.out.println(s);
System.out.println(t);
```
输出结果如下:
```
身份证号:330121200001010001,姓名:张三,年龄:18,性别:男,专业:计算机科学与技术,学号:1001,数学成绩:80,英语成绩:90,Java程序设计成绩:85
身份证号:330121197001010001,姓名:李四,年龄:30,性别:女,专业:计算机科学与技术,教师号:2001,授课学时数:20
```
阅读全文