设计抽象类person,派生出具体类:学生类student和教师类teacher,创建若干不同类对象后并在主方法中测试。\n数据成员定义:\nperson [id,姓名,生日]\nstudent [专业,成
时间: 2023-05-31 17:17:50 浏览: 352
### 回答1:
绩,班级]\nteacher [职称,工龄,所教科目]
设计抽象类person,包含数据成员[id,姓名,生日],并派生出具体类:学生类student和教师类teacher。学生类包含数据成员[专业,成绩,班级],教师类包含数据成员[职称,工龄,所教科目]。可以创建若干不同类对象,并在主方法中进行测试。
### 回答2:
设计抽象类person的作用是为学生类student和教师类teacher提供一个基础模板。抽象类本身不能被实例化,只能通过派生类来创建对象。而派生类则继承了抽象类的所有属性和方法,并可以根据自己的需求进行扩展和重载。
在本题中,person抽象类的数据成员定义包括id、姓名和生日等基本信息。student类继承了person类,同时增加了专业和成绩等具体属性。teacher类也继承了person类,但其具体属性为教授课程和薪水等。
在创建不同类对象后,可以通过主方法进行测试。例如,可以创建一个学生对象并设置其姓名、生日、专业和成绩等属性,然后输出这些信息。同时,也可以创建一个教师对象并设置其姓名、生日、教授课程和薪水等属性,并进行输出测试。
最后,需要注意的是抽象类的设计要尽可能做到简洁、通用和规范,以便后续的派生类进行更好的适配和扩展。同时,也要注意在派生类中只添加与基础类不同的属性和方法,避免过多的代码重复和资源浪费。正确的抽象类设计和派生类扩展对于程序的可维护性和可扩展性都至关重要。
### 回答3:
绩]\nteacher [职称,薪资]。
抽象类Person包含ID、姓名和生日三个数据成员和get/set方法。由于Person是抽象类,它不能被实例化,只能被继承。
学生类Student从Person类继承,增加了专业和成绩两个数据成员,也有相应的get/set方法。在主方法中,我们创建了多个不同专业、不同成绩的学生对象,并对其进行测试,比如查看学生的ID、姓名、生日、专业和成绩等信息。
教师类Teacher也从Person类继承,增加了职称和薪资两个数据成员,也有相应的get/set方法。在主方法中,我们同样创建了多个不同职称、不同薪资的教师对象,并对其进行测试,比如查看教师的ID、姓名、生日、职称和薪资等信息。
代码实现如下:
abstract class Person {
private int id;
private String name;
private String birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return this.birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
class Student extends Person {
private String major;
private int score;
public String getMajor() {
return this.major;
}
public void setMajor(String major) {
this.major = major;
}
public int getScore() {
return this.score;
}
public void setScore(int score) {
this.score = score;
}
}
class Teacher extends Person {
private String title;
private double salary;
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public double getSalary() {
return this.salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
public class AbstractClassExample {
public static void main(String[] args) {
Student student1 = new Student();
student1.setId(1001);
student1.setName("Tom");
student1.setBirthday("1999-01-01");
student1.setMajor("Computer Science");
student1.setScore(90);
Student student2 = new Student();
student2.setId(1002);
student2.setName("Alice");
student2.setBirthday("1998-01-01");
student2.setMajor("Mathematics");
student2.setScore(80);
Teacher teacher1 = new Teacher();
teacher1.setId(2001);
teacher1.setName("Richard");
teacher1.setBirthday("1970-01-01");
teacher1.setTitle("Professor");
teacher1.setSalary(100000);
Teacher teacher2 = new Teacher();
teacher2.setId(2002);
teacher2.setName("Mary");
teacher2.setBirthday("1980-01-01");
teacher2.setTitle("Assistant Professor");
teacher2.setSalary(80000);
// test students
System.out.println("Student 1:");
System.out.println("ID: " + student1.getId());
System.out.println("Name: " + student1.getName());
System.out.println("Birthday: " + student1.getBirthday());
System.out.println("Major: " + student1.getMajor());
System.out.println("Score: " + student1.getScore());
System.out.println();
System.out.println("Student 2:");
System.out.println("ID: " + student2.getId());
System.out.println("Name: " + student2.getName());
System.out.println("Birthday: " + student2.getBirthday());
System.out.println("Major: " + student2.getMajor());
System.out.println("Score: " + student2.getScore());
System.out.println();
// test teachers
System.out.println("Teacher 1:");
System.out.println("ID: " + teacher1.getId());
System.out.println("Name: " + teacher1.getName());
System.out.println("Birthday: " + teacher1.getBirthday());
System.out.println("Title: " + teacher1.getTitle());
System.out.println("Salary: " + teacher1.getSalary());
System.out.println();
System.out.println("Teacher 2:");
System.out.println("ID: " + teacher2.getId());
System.out.println("Name: " + teacher2.getName());
System.out.println("Birthday: " + teacher2.getBirthday());
System.out.println("Title: " + teacher2.getTitle());
System.out.println("Salary: " + teacher2.getSalary());
System.out.println();
}
}
阅读全文