我有一个包含两个子类的抽象类课程。
我需要实现加入/删除方法,这些方法基本上是从在线课程或大学课程分配或辞职的学生。
我如何从课程中实现抽象的join / drop方法两个子类?
然后在“学生课堂”中,我可以在大学/在线课程中创建一个新主题,然后将其返回。
感谢您的帮助,这是我的代码:
课程
public abstract class Course {
public String title;
public String description;
public LocalDate examDate;
public List<Lecture> lectures;
public abstract Course join();
public abstract Course drop();
}
class OnlineCourse extends Course{
public URL livestreamUrl;
public Course join() {
System.out.println("joined the online course");
}
public Course drop() {
System.out.println("dropped out of the online course");
}
}
class UniversityCourse extends Course{
public String roomName;
public Course join() {
System.out.println("joined the university-course");
}
public Course drop() {
System.out.println("dropped out of the university-course");
}
}
班级学生
public class Student {
public String matriculationNumber;
public String name;
public int age;
public Course study() {
// TODO 4: Comment the code below back in
// Change the Course type to OnlineCourse and set its title to "EIST"
// return the new course
// Course course = new Course();
// course.join();
// return course;
UniversityCourse EIST = new UniversityCourse();
EIST.join();
return EIST;
}
}
您可以在课程中添加学生的list
,并在加入/删除过程中添加/删除像这样:
public abstract class Course {
public String title;
public String description;
public LocalDate examDate;
public List<Lecture> lectures;
protected List<Student> students;
public Course(){
// init
// ...
students = new ArrayList<>();
}
public abstract void join(Student student);
public abstract void drop(Student student);
}
class OnlineCourse extends Course {
public URL livestreamUrl;
public void join(Student student) {
students.add(student);
System.out.println("joined the online course");
}
public void drop(Student student) {
students.remove(student);
System.out.println("dropped out of the online course");
}
}
class UniversityCourse extends Course {
public String roomName;
public void join(Student student) {
students.add(student);
System.out.println("joined the university-course");
}
public void drop(Student student) {
students.remove(student);
System.out.println("dropped out of the university-course");
}
}