Array withother classes - object - Java

2019-08-30 10:14发布

问题:

I have 3 classes: Course, CourseEntry and Transcript. In transcript, I have a function to add courses, like that:

public class Transcript {
    CourseEntry coursestaken[] = new CourseEntry[6];

    public void addCourse(Course course)
    {
        coursestaken[lastIndexOf(getCoursestaken())] = new CourseEntry(course);
    }
    (lastIndexOf gives me the empty array index - it's working on)

And in my CourseEntry:

public class CourseEntry {
    Course course;
    char grade = 'I';

    public CourseEntry(Course course)
    {
        this.course = course;
    }

And in my Course:

public  class Course {
    int courseNumber,credits;
    String courseName;

    public Course addNewCourse(int courseNumber, int credits, String courseName)
    {
        this.courseNumber = courseNumber;
        this.credits = credits;
        this.courseName = courseName;

        return this;
    }

In my main:

Transcript t = new Transcript();
Course course = new Course();

Course matematik = course.addNewCourse(1, 2, "Matematik");
t.addCourse(matematik);

Course turkce = course.addNewCourse(1, 4, "Türkçe");
t.addCourse(turkce);

But if I loop coursestaken array, it prints the last inserted index for all.

How can I solve that?

Thanks

回答1:

Objects are references in Java, that is, pointers to the objects. So when you do:

Object a = new Object();
Object b = a;

You're NOT copying the whole object a to b, but copying the reference to a to b (the memory address). So both a and b are references to the object created by new.

Let's follow your code so you see what's happening:

Course course = new Course();
Course matematik = course.addNewCourse(1, 2, "Matematik");
    this.courseNumber = courseNumber;
    this.credits = credits;
    this.courseName = courseName;
    return this;

Here you modified course object. matematik now is also the same as course because it points to same object.

Course turkce = course.addNewCourse(1, 4, "Türkçe");

Here you modify course again. Now course, turkce and matematik are all referencing the same object, which you created first with Course course = new Course();.

I think most easy way to fix this is that you create a constructor with parameters:

public class Course {
...
     public Course(int courseNumber,int credits,String courseName) {
           this.courseNumber = courseNumber;
           this.credits = credits;
           this.courseName = courseName;
     }
}

and then

  Course matematik = new Course(1, 2, "Matematik");
  t.addCourse(matematik);

  Course turkce = new Course(1, 4, "Türkçe");
  t.addCourse(turkce);


回答2:

You need to create a new Course object for each course, your addNewCourse method only mutates the current Course object. Modify Course like so:

public class Course {
    private final int courseNumber;
    private final int credits;
    private final String courseName;

    public Course(int courseNumber, int credits, String courseName) {
        this.courseNumber = courseNumber;
        this.credits = credits;
        this.courseName = courseName;
    }

    public int getCourseNumber() {
        return courseNumber;
    }

    public int getCredits() {
        return credits;
    }

    public String getCourseName() {
        return courseName;
    }
}

And then use the following:

Transcript t = new Transcript();

Course matematik = new Course(1, 2, "Matematik");
t.addCourse(matematik);

Course turkce = new Course(1, 4, "Türkçe");
t.addCourse(turkce);