文件夹名称:model 源码文件名称:scoreSystem.java 源代码:
package model; abstract class Person { //私有字段(后面部分private同理) private String name, gender, birth, academy; //有参构造函数 public Person(String name, String gender, String birth, String academy) { this.name = name; this.gender = gender; this.birth = birth; this.academy = academy; } //无参构造函数 public Person() { this.name = "empty_name"; this.gender = "empty_gender"; this.birth = "empty_birth"; this.academy = "empty_academy"; System.out.println("No_Parameter_Construction_Method_In_Person"); } //设置数据的公共方法 public void setData(String name, String gender, String birth, String academy) { this.name = name; this.gender = gender; this.birth = birth; this.academy = academy; } //如下代码开始获取各字段 public String getName() { return name; } public String getGender() { return gender; } public String getBirth() { return birth; } public String getAcademy() { return academy; } //打印所有字段信息的抽象方法,在继承中覆盖写入 public abstract void info(); } //管理员 class Admin extends Person { private String post; public String getPost() { return post; } public void setPost(String post) { this.post = post; } //无参构造函数 public Admin() { super(); //在开头调用父类Person的无参构造函数 post = "empty_post"; } //有参构造函数 public Admin(String n, String g, String b, String a, String p) { super(n, g, b, a); //传给父类有参构造函数 post = p; } //写入info(),打印所有字段 public void info() { System.out.println("Admin\n" + "Name:" + super.getName() + "\n" + "Gender: " + super.getGender() + "\n" + "Birth: " + super.getBirth() + "\n" + "Academy: " + super.getAcademy() + "\n" + "Post: " + getPost() + "\n"); } } //老师 class Teacher extends Person { private String teacherId, title; public String getTeacherId() { return teacherId; } public String getTitle() { return title; } public void setTeacherId(String id) { teacherId = id; } public void setTitle(String t) { title = t; } public Teacher() { super(); teacherId = "empty_teacherId"; title = "empty_title"; } public Teacher(String n, String g, String b, String a, String i, String t) { super(n, g, b, a); teacherId = i; title = t; } public void info() { System.out.println("Teacher\n" + "Name:" + super.getName() + "\n" + "Gender: " + super.getGender() + "\n" + "Birth: " + super.getBirth() + "\n" + "Academy: " + super.getAcademy() + "\n" + "TeacherId: " + getTeacherId() + "\n" + "Title: " + getTitle() + "\n"); } } //学生 class Student extends Person { private String studentId, major; public String getStudentId() { return studentId; } public String getMajor() { return major; } public void setStudentId(String s) { studentId = s; } public void setMajor(String m) { major = m; } public Student() { super(); studentId = "empty_studentId"; major = "empty_major"; } public Student(String n, String g, String b, String a, String s, String m) { super(n, g, b, a); studentId = s; major = m; } public void info() { System.out.println("Student\n" + "Name:" + super.getName() + "\n" + "Gender: " + super.getGender() + "\n" + "Birth: " + super.getBirth() + "\n" + "Academy: " + super.getAcademy() + "\n" + "StudentId: " + getStudentId() + "\n" + "major: " + getMajor() + "\n"); } } public class scoreSystem { public static void main(String[] args) { Person p1 = new Admin("ZengWX", "male", "200102", "software", "developer"); p1.info(); Person p2 = new Teacher("Zhang", "male", "1973", "software", "#1354", "professor"); p2.info(); Person p3 = new Student("DongLN", "female", "2001", "software", "08026", "system and software"); p3.info(); Person p4 = new Student(); p4.info(); } }