可比接口问题

时间:2013-10-05 19:47:52

标签: java interface comparable

您好我为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

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

1 个答案:

答案 0 :(得分:2)

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

compareTO

应该叫做

compareTo

然而,下次一定要仔细阅读错误信息,这通常是一个很好的提示,看看...在提出问题之前要做的另一件好事是将错误粘贴到任何搜索引擎中找到(即使在Stackoverflow)