嗨,我为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


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

最佳答案

Java完全区分大小写...您的方法称为

compareTO


应该叫

compareTo


但是,下次一定要仔细阅读错误消息,这通常是一个很好的提示。...问一个问题之前要做的另一件事是将错误复制并粘贴到您找到的任何搜索引擎中(甚至在Stackoverflow)

10-04 21:28