0

嗨,我为 Comparable 接口编写了简单的代码如下

import java.util.*;
class Gaurav123 implements Comparable<Gaurav123>
{
  String title;
  Gaurav123()
  {

  }

  Gaurav123(String title)
  {
    this.title=title;
  }

  public int compareTO(Gaurav123 b)
  {
    return title.compareTo(b.title);
  }
}

public class Gaurav1234
{

  public static void main(String [] args)
  {
    Gaurav123 g1=new Gaurav123("gaurav");
    Gaurav123 g2=new Gaurav123("Surbhi");
    Gaurav123 g3=new Gaurav123("Kailash");
    TreeSet<Gaurav123>ts=new TreeSet<Gaurav123>();
    ts.add(g1);
    ts.add(g2);
    ts.add(g3);
  }
}

但我收到了这个错误

C:\Users\gakaushik\Desktop>javac Gaurav1234.java
Gaurav1234.java:2: Gaurav123 is not abstract and does not override abstract meth
od compareTo(Gaurav123) in java.lang.Comparable
class Gaurav123 implements Comparable<Gaurav123>
^
1 error

任何想法是什么问题。我遵循了所有 Comparable 接口协议

4

1 回答 1

2

Java 完全区分大小写...您的方法被调用

compareTO

它应该被称为

compareTo

但是,下次一定要仔细阅读错误消息,它通常是一个很好的提示,看什么......在提问之前要做的另一件事是 copz 并将错误粘贴到您找到的任何搜索引擎中(甚至在堆栈溢出)

于 2013-10-05T19:52:42.767 回答