java 定义一个学生类,学生类的属性有姓名,学号,年龄,生日,专业,在学生类的基础上定义一个课程类,课程类的属性有课程号,课程名,课程性质,学时,学分;定义一个选课接口,由学生类实现该接口
时间: 2024-05-03 12:20:28 浏览: 13
ChitGPT:
好的,以下是定义学生类和课程类的代码:
```java
// 学生类
public class Student {
private String name;
private String studentID;
private int age;
private String birthday;
private String major;
public Student(String name, String studentID, int age, String birthday, String major) {
this.name = name;
this.studentID = studentID;
this.age = age;
this.birthday = birthday;
this.major = major;
}
// 这里实现选课接口
public interface CourseSelection {
void selectCourse(Course course);
}
}
// 课程类
public class Course {
private String courseID;
private String courseName;
private String courseNature;
private int courseHour;
private int courseCredit;
public Course(String courseID, String courseName, String courseNature, int courseHour, int courseCredit) {
this.courseID = courseID;
this.courseName = courseName;
this.courseNature = courseNature;
this.courseHour = courseHour;
this.courseCredit = courseCredit;
}
}
```
在学生类中实现选课接口的代码如下:
```java
public class Student implements CourseSelection {
private String name;
private String studentID;
private int age;
private String birthday;
private String major;
private List<Course> courseList;
public Student(String name, String studentID, int age, String birthday, String major) {
this.name = name;
this.studentID = studentID;
this.age = age;
this.birthday = birthday;
this.major = major;
this.courseList = new ArrayList<Course>();
}
public void selectCourse(Course course) {
courseList.add(course);
}
}
```
这样就完成了一个简单的选课功能,可以通过调用 selectCourse 方法来选课。