equals() method for classes with bidirectional association

前端 未结 4 1019
时光说笑
时光说笑 2021-01-17 22:06

I am trying to implement equals method for Java classes Book and Chapter in my application. Book has a set of Chapt

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-17 22:57

    From a modeling perspective, the chapter is part of the book. So although you have references in both directions, the book is "stronger" than the chapter.

    When you have part-of relationships like with Book and Chapter, the part (Chapter) sometimes takes the whole (Book) into account when defining equals(). But not the other way round.

    So clearly, the book would not use its chapters to define equals(). The chapter might use the book. That depends on the model.

提交回复
热议问题