Description 有学生和教师两类人员。学生信息有身份证号、学号、专业、姓名、性别、年龄及数学、英语、Java程序设计三门课程的成绩;教师信息有身份证号、教师号、专业、姓名、性别、年龄、职称及授课的时数。 定义学生类和教师类、创建类的对象并输出对象的信息。 要求:必须基于面向对象的三个特征编写程序!否则,本题的成绩为0分! Input 有多组数据。 每组数据的第1个数据是类型:1——学生,2——教师,其后是相应对象的各个信息,如Sample Input所示。 Output 输出学生或教师的信息。 Sample Input 1 1234567890 04010101 Yaoming m 20 Computer 90 85 80 2 9123456789 0401001 Zhang f 42 Computer middle-rank 90 Sample Output Student:1234567890 04010101 Yaoming m 20 Computer 90 85 80 Teacher:9123456789 0401001 Zhang f 42 Computer middle-rank 90 Hint 1.必须定义类和类的对象,并通过对象完成计算,不得在main方法中完成计算;
时间: 2024-04-01 16:36:16 浏览: 83
以下是基于面向对象三个特征的 Java 代码实现:
```java
import java.util.Scanner;
class Person {
private String idCard; // 身份证号
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
private String major; // 专业
// 构造函数
Person(String idCard, String name, String gender, int age, String major) {
this.idCard = idCard;
this.name = name;
this.gender = gender;
this.age = age;
this.major = major;
}
// 输出个人信息
void printInfo() {
System.out.println(idCard + " " + name + " " + gender + " " + age + " " + major);
}
}
class Student extends Person {
private String studentId; // 学号
private int mathScore; // 数学成绩
private int englishScore; // 英语成绩
private int javaScore; // Java程序设计成绩
// 构造函数
Student(String idCard, String studentId, String name, String gender, int age, String major, int mathScore, int englishScore, int javaScore) {
super(idCard, name, gender, age, major);
this.studentId = studentId;
this.mathScore = mathScore;
this.englishScore = englishScore;
this.javaScore = javaScore;
}
// 输出学生信息
void printInfo() {
System.out.print("Student:");
super.printInfo();
System.out.println(studentId + " " + mathScore + " " + englishScore + " " + javaScore);
}
}
class Teacher extends Person {
private String teacherId; // 教师号
private String title; // 职称
private int teachingHours; // 授课的时数
// 构造函数
Teacher(String idCard, String teacherId, String name, String gender, int age, String major, String title, int teachingHours) {
super(idCard, name, gender, age, major);
this.teacherId = teacherId;
this.title = title;
this.teachingHours = teachingHours;
}
// 输出教师信息
void printInfo() {
System.out.print("Teacher:");
super.printInfo();
System.out.println(teacherId + " " + title + " " + teachingHours);
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int type = scanner.nextInt();
if (type == 1) {
String idCard = scanner.next();
String studentId = scanner.next();
String name = scanner.next();
String gender = scanner.next();
int age = scanner.nextInt();
String major = scanner.next();
int mathScore = scanner.nextInt();
int englishScore = scanner.nextInt();
int javaScore = scanner.nextInt();
Student student = new Student(idCard, studentId, name, gender, age, major, mathScore, englishScore, javaScore);
student.printInfo();
} else if (type == 2) {
String idCard = scanner.next();
String teacherId = scanner.next();
String name = scanner.next();
String gender = scanner.next();
int age = scanner.nextInt();
String major = scanner.next();
String title = scanner.next();
int teachingHours = scanner.nextInt();
Teacher teacher = new Teacher(idCard, teacherId, name, gender, age, major, title, teachingHours);
teacher.printInfo();
}
}
scanner.close();
}
}
```
代码中定义了一个 Person 类作为学生和教师类的基类,这样可以减少代码的冗余。学生类和教师类都继承自 Person 类,并在此基础上添加了各自的属性和方法。在主函数中,根据输入的类型创建相应的学生类或教师类的对象,并输出对象信息。注意,输出格式要与题目要求一致。
阅读全文