嗨,我为 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 接口协议