2013-01-15 140 views
0

我有3个类:Course,CourseEntryTranscript。在谈话,我有一个函数来添加课程,这样的:其他类的对象 - 对象 - Java

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) 

在我CourseEntry

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

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

在我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; 
    } 

在我的主:

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); 

但是,如果我循环coursestaken数组,它会打印最后插入的所有索引。

我该如何解决呢?

感谢

+5

'Course.addNewCourse' *变异/修改/更新*当前对象(并不会“添加”到任何东西)。相反,删除该方法并使用'new Course'(构造函数应更新为courseNumber,credits,courseName),然后将* new * Course对象添加到Transcript。 – 2013-01-15 21:02:16

+0

请包括循环代码以及堆栈跟踪 – amphibient

+0

哇读,代码真的伤害了我的大脑 –

回答

3

对象是引用在Java中,也就是说,指针指向的对象。所以,当你这样做:

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

你不是复制整个对象ab,但复制引用ab(内存地址)。因此ab都是对由new创建的对象的引用。

让我们跟随你的代码,所以你看到发生了什么:

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

在这里,您修改course对象。 matematik现在也与course相同,因为它指向相同的对象。

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

在这里,您再次修改course。现在,course,turkcematematik都引用了您首先使用Course course = new Course();创建的相同对象。

我认为最简单的方法来解决这个问题是您创建一个带参数的构造函数:

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

然后

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

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

谢谢大家,非常感谢。但是,我想知道,使用这个新的东西来创建一个新的对象,将会在内存或任何地方使用更多的空间。这是最好的方式(用于表演)还是最简单的方式? –

+0

不客气。学习Java时这是一个常见的错误。是的,它需要更多的内存,如果可以的话,你应该避免做“新”。然而,在你的情况下,你不能避免创建一个新的对象,因为你想每次存储不同的数据并保存它,所以你添加的数据越多,你使用的内存就越多,显然:)不用担心,计算机这一天可以采取比这更多;) – m0skit0

+0

感谢所有:) –

7

你需要创建一个新的Course对象为每门课程,你addNewCourse方法只变异当前Course对象。修改Course像这样:

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; 
    } 
} 

然后使用以下命令:

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); 
+0

谢谢大家,真的很感谢。但是,我想知道,使用这个新的东西来创建一个新的对象,将会在内存或任何地方使用更多的空间。这是最好的方式(用于表演)还是最简单的方式? –

+0

@MuhammetArslan除了为每个课程分配一个对象之外,没有其他方法可以做你正在描述的内容。 –