用java定义一个学生类和一个课程类,要求能实现一个学生能选择多门课程的情况
2个回答
展开全部
import java.util.ArrayList;
import java.util.List;
/**
* @stereotype role
*/
public class Student {
private String name;
private java.util.List<Course> courses = new ArrayList<Course>();
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getAssignedCourseCount() {
return courses.size();
}
public List<Course> getAllAssignedCourses() {
return courses;
}
public void addCourse(Course course) {
courses.add(course);
}
public static void main(String[] args) {
Course[] courses = Course.getAvailableCourses();
System.out.println("以下是可供学生选择的课程列表");
System.out.println("课程名称");
for (int i = 0; i < courses.length; i++)
System.out.println(courses[i].getName());
System.out.println("================================");
Student student = new Student("张三");
student.addCourse(courses[0]);
student.addCourse(courses[2]);
System.out.println("学生:"+student.getName()+"已选择了"+student.getAssignedCourseCount()+"门课程:");
for(Course course:student.getAllAssignedCourses()){
System.out.println(course.getName());
}
}
}
class Course {
private String name;
public Course(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static Course[] getAvailableCourses() {
return new Course[] { new Course("数学"), new Course("语文"),
new Course("英语") };
}
}
import java.util.List;
/**
* @stereotype role
*/
public class Student {
private String name;
private java.util.List<Course> courses = new ArrayList<Course>();
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getAssignedCourseCount() {
return courses.size();
}
public List<Course> getAllAssignedCourses() {
return courses;
}
public void addCourse(Course course) {
courses.add(course);
}
public static void main(String[] args) {
Course[] courses = Course.getAvailableCourses();
System.out.println("以下是可供学生选择的课程列表");
System.out.println("课程名称");
for (int i = 0; i < courses.length; i++)
System.out.println(courses[i].getName());
System.out.println("================================");
Student student = new Student("张三");
student.addCourse(courses[0]);
student.addCourse(courses[2]);
System.out.println("学生:"+student.getName()+"已选择了"+student.getAssignedCourseCount()+"门课程:");
for(Course course:student.getAllAssignedCourses()){
System.out.println(course.getName());
}
}
}
class Course {
private String name;
public Course(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static Course[] getAvailableCourses() {
return new Course[] { new Course("数学"), new Course("语文"),
new Course("英语") };
}
}
展开全部
课程类:
public class Course {
private Integer id;
private String name;
// 其他属性自己添加,并省去get和set方法
}
学生类:
import java.util.HashSet;
import java.util.Set;
public class Student {
private Integer id;
private String name;
// 其他属性自己添加
private Set<Course> courses = new HashSet<Course>();
// 以下省去get和set方法
}
public class Course {
private Integer id;
private String name;
// 其他属性自己添加,并省去get和set方法
}
学生类:
import java.util.HashSet;
import java.util.Set;
public class Student {
private Integer id;
private String name;
// 其他属性自己添加
private Set<Course> courses = new HashSet<Course>();
// 以下省去get和set方法
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询