java 编写一个学生和教师数据输入和显示程序学生数据有编号、姓名、班号和成绩教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类Person并作为学生数据操作类Student和教师数据操作类Teacher的基类。输出如下结果: 。
以下是Java中的一个实现示例:
class Person {
private int id;
private String name;
public Person(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
class Student extends Person {
private int classNumber;
private int score;
public Student(int id, String name, int classNumber, int score) {
super(id, name);
this.classNumber = classNumber;
this.score = score;
}
public int getClassNumber() {
return classNumber;
}
public int getScore() {
return score;
}
}
class Teacher extends Person {
private String title;
private String department;
public Teacher(int id, String name, String title, String department) {
super(id, name);
this.title = title;
this.department = department;
}
public String getTitle() {
return title;
}
public String getDepartment() {
return department;
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student(1, "John", 1, 90);
Teacher teacher = new Teacher(2, "Jane", "Professor", "Computer Science");
System.out.println("Student Information:");
System.out.println("ID: " + student.getId());
System.out.println("Name: " + student.getName());
System.out.println("Class Number: " + student.getClassNumber());
System.out.println("Score: " + student.getScore());
System.out.println("\nTeacher Information:");
System.out.println("ID: " + teacher.getId());
System.out.println("Name: " + teacher.getName());
System.out.println("Title: " + teacher.getTitle());
System.out.println("Department: " + teacher.getDepartment());
}
}
输出结果:
Student Information:
ID: 1
Name: John
Class Number: 1
Score: 90
Teacher Information:
ID: 2
Name: Jane
Title: Professor
Department: Computer Science
``
原文地址: http://www.cveoy.top/t/topic/hC5t 著作权归作者所有。请勿转载和采集!