具有双向关联的类的 equals() 方法

发布于 2024-11-10 06:03:01 字数 1535 浏览 5 评论 0原文

我正在尝试在我的应用程序中为 Java 类 BookChapter 实现 equals 方法。 Book 有一组Chapter,而Chapter 则有一个关联的Book。双向关联如下所示:

class Book{
    private String isbn;
    private String name;
    private Date publishDate;
    private Set<Chapter> chapters;
    ...

    public boolean equals(Object o){
        if(o == this){
            return true;
        }
        if (!(o instanceof Book)){
            return false;
        }
        Book book = (Book)o;
        if( (this.isbn.equals(book.getIsbn()) ) && (this.name.equals(book.getName())) &&(this.publishDate.equals(book.getPublishDate())) &&(this.chapters.equals(book.getChapters())) ){
            return true;
        }else{
            return false;
        }

    }
}

现在我尝试为 Chapter 实现 equals

public class Chapter {
    private String title;
    private Integer noOfPages;
    private Book book;
    ...

    public boolean equals(Object o){
         if(o == this){
            return true;
        }
        if (!(o instanceof Chapter)){
            return false;
        }
        Chapter ch = (Chapter)o;
        if((this.title.equals(book.getTitle())) && (this.noOfPages.intValue()== book.getNoOfPages().intValue())  ){
            return true;
        }else{
            return false;
        }
    }

}

这里,我想知道是否也需要比较 book 字段。这不会开始无限循环吗?对于这种双向关联,实现 equals 方法的正确方法是什么?

I am trying to implement equals method for Java classes Book and Chapter in my application. Book has a set of Chapters, while a Chapter has an associated Book. The bidirectional association is shown as below:

class Book{
    private String isbn;
    private String name;
    private Date publishDate;
    private Set<Chapter> chapters;
    ...

    public boolean equals(Object o){
        if(o == this){
            return true;
        }
        if (!(o instanceof Book)){
            return false;
        }
        Book book = (Book)o;
        if( (this.isbn.equals(book.getIsbn()) ) && (this.name.equals(book.getName())) &&(this.publishDate.equals(book.getPublishDate())) &&(this.chapters.equals(book.getChapters())) ){
            return true;
        }else{
            return false;
        }

    }
}

Now I tried to implement equals for Chapter:

public class Chapter {
    private String title;
    private Integer noOfPages;
    private Book book;
    ...

    public boolean equals(Object o){
         if(o == this){
            return true;
        }
        if (!(o instanceof Chapter)){
            return false;
        }
        Chapter ch = (Chapter)o;
        if((this.title.equals(book.getTitle())) && (this.noOfPages.intValue()== book.getNoOfPages().intValue())  ){
            return true;
        }else{
            return false;
        }
    }

}

Here, I am wondering if I need to compare the book field as well. Wouldn't that start an infinite loop? What is the correct way of implementing the equals method for such bidirectional associations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白昼 2024-11-17 06:03:03

您必须选择 Book 字段作为 ID(如 ISBN)。然后,在 Chapter equals 中,您可以执行类似的操作

book.getISBN().equals(other.book.getISBN())

You have to choose a Book field as a ID (like ISBN). Then, in Chapter equals, you can do a thing like

book.getISBN().equals(other.book.getISBN())
半窗疏影 2024-11-17 06:03:02

仅当一本书的 ISBN 相同时,它们才应与另一本书相同。因此,仅基于该领域来实现本书的 equals。

对于章节 - 比较章节编号和所属的书籍

A book should be equal to another book only if their ISBNs are equal. So implement the book equals only based on that field.

For the chapter - compare the chapter number and the owning Book

陈年往事 2024-11-17 06:03:02

(我假设这是Java)
在 Chapter 类的 equals 方法中,您可以只比较书籍参考文献(即使用 ==,而不是 equals)。这仅比较引用,因此可以避免无限循环。但是,如果您有时克隆书籍,这种方法就会失败。

解决这种特定情况的更好方法是不比较书籍,而是比较它们的 ISBN,因为这是一本书的唯一标识符。

一般来说,最好避免这样的双向依赖。一种方法是让两个类之一实现一个接口,以免直接使用它。

(I'm assuming this is Java)
In the Chapter class equals method, you could just compare the book references (that is, using ==, not equals). This only compare references, so it would avoid an infinite loop. However, if you Clone books sometimes, this approach would fail.

An even better way to solve this specific case would be to compare not the books, but their ISBN, since that is an unique identifier for a Book.

In general, it is better to avoid bidirectional dependencies like this. One way is to have one of the two classes implement an interface, so as not to use it directly.

蝶…霜飞 2024-11-17 06:03:02

从建模的角度来看,本章是本书的一部分。因此,尽管你有两个方向的参考资料,但这本书比章节“更强”。

当您具有像 Book 和 Chapter 那样的部分关系时,在定义 equals() 时,部分(Chapter)有时会考虑整个(Book)。但反之则不然。

很明显,本书不会使用其章节来定义 equals()。该章可能会使用本书。这取决于型号。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文