设计一个Java学生信息读取器,希望所设计的读取器可以展示学生的学号和名字,分别采用面向抽象编程和面向接口的编程实现。其中:除了主类外,还有学生类Student和三类学生,即本科生(Undergraduate)、硕士生(Master Degree Candidate)和博士生(Doctoral Candidate),以及的学生信息读取器StuInfoReader和大学(University)。其中:大学负责学生相关信息的设定。
时间: 2024-01-24 07:16:59 浏览: 70
面向抽象编程实现:
1. 定义抽象类Person,包含属性id和name,以及对应的get方法。
```
public abstract class Person {
private String id;
private String name;
public Person(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}
```
2. 定义学生类Student,继承自Person类,添加属性grade和major,以及对应的get方法。
```
public class Student extends Person {
private int grade;
private String major;
public Student(String id, String name, int grade, String major) {
super(id, name);
this.grade = grade;
this.major = major;
}
public int getGrade() {
return grade;
}
public String getMajor() {
return major;
}
}
```
3. 定义三类学生类,分别继承自Student类。
```
public class Undergraduate extends Student {
public Undergraduate(String id, String name, int grade, String major) {
super(id, name, grade, major);
}
}
public class MasterDegreeCandidate extends Student {
public MasterDegreeCandidate(String id, String name, int grade, String major) {
super(id, name, grade, major);
}
}
public class DoctoralCandidate extends Student {
public DoctoralCandidate(String id, String name, int grade, String major) {
super(id, name, grade, major);
}
}
```
4. 定义接口StuInfoReader,包含方法readStuInfo,用于读取学生信息。
```
public interface StuInfoReader {
public void readStuInfo();
}
```
5. 定义大学类University,实现StuInfoReader接口,负责学生相关信息的设定和读取。
```
public class University implements StuInfoReader {
private List<Student> students = new ArrayList<>();
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public void readStuInfo() {
for(Student s : students) {
System.out.println("学号:" + s.getId() + ",姓名:" + s.getName());
}
}
}
```
6. 编写主类,创建大学对象和学生对象,调用readStuInfo方法输出学生信息。
```
public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Undergraduate("001", "张三", 2, "计算机科学与技术"));
students.add(new MasterDegreeCandidate("002", "李四", 1, "软件工程"));
students.add(new DoctoralCandidate("003", "王五", 3, "人工智能"));
University university = new University();
university.setStudents(students);
university.readStuInfo();
}
}
```
面向接口编程实现:
1. 定义接口Person,包含方法getId和getName。
```
public interface Person {
public String getId();
public String getName();
}
```
2. 定义学生类Student,实现Person接口,添加属性grade和major,以及对应的get方法。
```
public class Student implements Person {
private String id;
private String name;
private int grade;
private String major;
public Student(String id, String name, int grade, String major) {
this.id = id;
this.name = name;
this.grade = grade;
this.major = major;
}
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
public int getGrade() {
return grade;
}
public String getMajor() {
return major;
}
}
```
3. 定义三类学生类,实现Person接口。
```
public class Undergraduate implements Person {
private String id;
private String name;
public Undergraduate(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
}
public class MasterDegreeCandidate implements Person {
private String id;
private String name;
public MasterDegreeCandidate(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
}
public class DoctoralCandidate implements Person {
private String id;
private String name;
public DoctoralCandidate(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
}
```
4. 定义接口StuInfoReader,包含方法readStuInfo,用于读取学生信息。
```
public interface StuInfoReader {
public void readStuInfo(List<Person> persons);
}
```
5. 定义大学类University,实现StuInfoReader接口,负责学生相关信息的设定和读取。
```
public class University implements StuInfoReader {
@Override
public void readStuInfo(List<Person> persons) {
for(Person p : persons) {
System.out.println("学号:" + p.getId() + ",姓名:" + p.getName());
}
}
}
```
6. 编写主类,创建大学对象和学生对象,调用readStuInfo方法输出学生信息。
```
public class Main {
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
persons.add(new Undergraduate("001", "张三"));
persons.add(new MasterDegreeCandidate("002", "李四"));
persons.add(new DoctoralCandidate("003", "王五"));
University university = new University();
university.readStuInfo(persons);
}
}
```
阅读全文