gpt4 book ai didi

java - 类似的接口(interface)问题

转载 作者:行者123 更新时间:2023-12-02 06:40:24 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

compareTO

应该叫它

compareTo

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

关于java - 类似的接口(interface)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19201979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com