0

我见过其他数组代码,但这是我得到的。我正在尝试在数组中的 CollegeCollege 课程中设置 courseID。我需要在 student.java 中设置数组

public class CollegeCourse {

private static String courseID;
private static int creditHours;
private static char grade;

public static void setCourseID(String course){
    courseID = course;
}
public static String getCourseID(){
    return courseID;
}
public static void setCreditHours(int CH){
    creditHours = CH;
}
public static int getCreditHours(){
    return creditHours;
}
public static void setGrade(char g){
    grade = g;
}
public static char getGrade(){
    return grade;
}


public class Student {

private static int IDnumber;
private static CollegeCourse[] course = new CollegeCourse[5];

public static void setIDnumber(int x){
    IDnumber = x;
}
public static int getIDnumber(){
    return IDnumber;
}
public static String getCourse(int x){
    return course[x].getCourseID();
}
public static void setCourse(CollegeCourse newCourse, int ID){
    CollegeCourse.setCourseID = newCourse[ID];
}
}
4

2 回答 2

0

看起来您正在尝试编写学生使用 id 5 进行课程的代码。也许您的课程应该有构造函数根据课程的 ID 来接受参数。

而且我不知道您需要将字段设为静态的原因,如果出于某种原因需要,请随意将它们设为静态。

public class CollegeCourse {

private String courseID;
private int creditHours;
private char grade;

public CollegeCourse(String courseID) {
    this.courseID = courseID;
}

...

public class Student {

...
private static CollegeCourse[] course = new CollegeCourse("5");

...
于 2013-03-27T23:42:34.810 回答
0

我认为更好的设计是这样的:

public class CollegeCourse {

    private final String courseId;

    private final int creditHours;

    public CollegeCourse(final String courseId, int creditHours) {
        this.courseId = courseId;
        this.creditHours = creditHours;
    }

    public String getCourseId() {
        return courseId;
    }

    public int getCreditHours() {
        return creditHours;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) { return true; }
        if (!(o instanceof CollegeCourse)) { return false; }
        CollegeCourse cc = (CollegeCourse)o;
        return courseId.equals(cc.courseId) 
            && creditHours == cc.creditHours;                   
    }

    @Override
    public int hashCode() {
       int result = 17;
       result = 31 * result + courseId.hashCode();
       result = 31 * result + creditHours;
       return result;
    }
};

public enum Grade { A, B, C, D, E };

public class Student {

    private final String id;

    private final Map<CollegeCourse, Grade> course2GradeMap = new HashMap<>();

    public Student(final String id) {
        this.id = id;       
    }

    public Grade getGrade(final CollegeCourse course) {
        return couse2GradeMap.get(course);
    }

    public void addCollegeCourse(final CollegeCourse course, Grade grade) { 
        course2GradeMap.put(course, grade);
    }

    public Collection<CollegeCourse> getCollegeCourses() {
        return Collections.unmodifiableCollection(course2GradeMap.values());
    }
};

以这种方式使用它:

Student student = new Student("001");
student.addCollegeCourse(new CollegeCourse('alg', 100), Grade.A);
student.addCollegeCourse(new CollegeCourse('stat', 100), Grade.C);
于 2013-03-27T23:59:16.740 回答