我不明白我们如何在方法 compareTo 中使用方法 compareTo

我正在做这个项目,在那里我会得到一些书,我应该按照这个逻辑对它进行排序:

  1. 如果一本书的页数比另一本书的页数多,那么页数多的书排在第一位。

  2. 如果两本书的页数相同,则按标题字母顺序排序。

  3. 如果两本书的页数和标题相同,则按作者字母顺序排序。

我试图解决但没有成功,所以我在解决方案中查找:

public class Book implements Comparable<Book>{


   public int compareTo(Book specifiedBook) {

      // First check if they have different page counts


      if(this.numberOfPages != specifiedBook.numberOfPages){


         // this will return a negative value if this < specified but will return a positive value if this > specified

         return this.numberOfPages - specifiedBook.numberOfPages;

      }


      // If page counts are identical then check if the titles are different

      if(!this.title.equals(specifiedBook.title){


         return this.title.compareTo(specifiedBook.title);

      }


      // If page titles are also identical then return the comparison of the authors


      return this.author.compareTo(specifiedBook.author);

    }

}

我在这里不明白的是这部分“return this.title.compareTo(specifiedBook.title);”。我们如何在方法 compareTo 中使用方法 compareTo,这不是递归吗,我知道当我们实现接口 comperable 时,我们必须覆盖方法 compareTo,但是我们如何在覆盖的 compareTo 方法中使用该 compareTo 方法?在这种情况下和一般情况下,它应该做什么?


令我困惑的是,我们如何使用刚刚在接口中声明的方法,它不是从父类继承的。它只是在可比接口中声明,所以当我们实现接口时,我们必须重写它


阿晨1998
浏览 99回答 2
2回答

慕虎7371278

假设title和author是字符串,您正在String.compareTo从内部调用(已经实现)Book.compareTo。这不是递归。这是不同类中的方法。

SMILET

使用 java.util.Comparator 比较字段的最简单和更好的方法。我强烈建议覆盖 equals 和 hashCode 方法,因为您要将书籍放在某个容器中,该容器可能是 Sorted Set 或类似的东西。无论如何,这是示例代码,import org.junit.Test;import java.util.Comparator;import java.util.Objects;import java.util.StringJoiner;import java.util.TreeSet;public class BookTest {&nbsp; @Test&nbsp; public void compareBooks() {&nbsp; &nbsp; Book b1 = new Book(100, "A book", "Zoro");&nbsp; &nbsp; Book b2 = new Book(10, "Small book", "ABC");&nbsp; &nbsp; TreeSet<Book> books = new TreeSet<>();&nbsp; &nbsp; books.add(b1);&nbsp; &nbsp; books.add(b2);&nbsp; &nbsp; System.out.println(books);&nbsp; }&nbsp; private class Book implements Comparable<Book> {&nbsp; &nbsp; private final int numberOfPages;&nbsp; &nbsp; private final String title;&nbsp; &nbsp; private final String author;&nbsp; &nbsp; private Book(int numberOfPages, String title, String author) {&nbsp; &nbsp; &nbsp; this.numberOfPages = numberOfPages;&nbsp; &nbsp; &nbsp; this.title = title;&nbsp; &nbsp; &nbsp; this.author = author;&nbsp; &nbsp; }&nbsp; &nbsp; public int getNumberOfPages() {&nbsp; &nbsp; &nbsp; return numberOfPages;&nbsp; &nbsp; }&nbsp; &nbsp; public String getTitle() {&nbsp; &nbsp; &nbsp; return title;&nbsp; &nbsp; }&nbsp; &nbsp; public String getAuthor() {&nbsp; &nbsp; &nbsp; return author;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compareTo(Book that) {&nbsp; &nbsp; &nbsp; return Comparator.nullsFirst(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Comparator.comparing(Book::getNumberOfPages)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .thenComparing(Book::getTitle)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .thenComparing(Book::getAuthor))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .compare(this, that);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean equals(Object o) {&nbsp; &nbsp; &nbsp; if (this == o) return true;&nbsp; &nbsp; &nbsp; if (o == null || getClass() != o.getClass()) return false;&nbsp; &nbsp; &nbsp; Book book = (Book) o;&nbsp; &nbsp; &nbsp; return numberOfPages == book.numberOfPages&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && title.equals(book.title)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && author.equals(book.author);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int hashCode() {&nbsp; &nbsp; &nbsp; return Objects.hash(numberOfPages, title, author);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; return new StringJoiner(", ", Book.class.getSimpleName() + "[", "]")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add("numberOfPages=" + numberOfPages)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add("title='" + title + "'")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add("author='" + author + "'")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toString();&nbsp; &nbsp; }&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java