重写抽象方法compare()
您好,我对编程相当陌生,我在比较方法方面遇到困难,我有几个类,我最初的问题是我的父类。
我收到此错误:
Person 不是抽象的,并且不会覆盖比较器中的方法比较(java.lang.Object,java.lang.Object)
public class Person implements Comparator
{
//some methods
public int compare(Person p1, Person p2)
{
// if last names are the same compare first names
if(p1.getLastName().equals(p2.getLastName()))
{
return p1.getFirstName().compareTo(p2.getFirstName());
}
return p1.getLastName().compareTo(p2.getLastName());
}
我的子类看起来像这样:
public class Player extends Person implements Comparator
{
//some methods
public int compare(Player p1, Player p2)
{
if(p1.getGamesPlayed()<p2.getGamesPlayed())
{
return -1;
}else if (p1.getGamesPlayed()==p2.getGamesPlayed())
{
return 0;
}else
{
return 1;
}
}
我还有一个俱乐部类,它将所有信息存储在一个 ArrayList
团队。
我的界面:
public interface Comparator<T>
{
int compare(T o1, T o2);
}
我也有这个类,
public class ComparePlayers implements Comparator<Player>
{
public int compare(Player p1, Player p2)
{
if(p1.getGamesPlayed()< p2.getGamesPlayed())
{
return -1;
}else if(p1.getGamesPlayed()== p2.getGamesPlayed())
{
return p1.getLastName().compareTo(p2.getLastName());
}else
{
return 1;
}
}
其规范是:
当一个新球员签约时,她/他应该按姓氏的字母顺序插入到 Club 类中(如果姓氏相同,则按名字排列)。为此,请让您的 Person 和 Player 类实现适当的 Comparable 接口。
编写一个实现 Comparator
接口的类 ComparePlayers。它应该根据所玩的游戏数量来比较玩家(如果所玩的游戏数量相同,则按姓氏的字母顺序进行比较)。为 Club 类实现一个新的构造函数,该构造函数采用 Comparator
参数。因此,编写一个主程序,该程序将打印有关俱乐部中每个球员的信息,其中球员按所玩比赛的降序排列。这应该允许主程序指定顺序,而无需修改任何其他类中的代码。
如果这很啰嗦,我很抱歉,但我正在努力找出问题所在,我尝试了几种变体,但它不起作用。该报告将于周五发布,只要朝着正确的方向推动,我们将不胜感激。
Hi I'm reasonably new to programming and I am having difficulty with my compare method, I have several classes, my initial issue is with my parent class.
I get this error:
Person is not abstract and does not override method compare(java.lang.Object, java.lang.Object) in Comparator
public class Person implements Comparator
{
//some methods
public int compare(Person p1, Person p2)
{
// if last names are the same compare first names
if(p1.getLastName().equals(p2.getLastName()))
{
return p1.getFirstName().compareTo(p2.getFirstName());
}
return p1.getLastName().compareTo(p2.getLastName());
}
My child class looks some thing like this:
public class Player extends Person implements Comparator
{
//some methods
public int compare(Player p1, Player p2)
{
if(p1.getGamesPlayed()<p2.getGamesPlayed())
{
return -1;
}else if (p1.getGamesPlayed()==p2.getGamesPlayed())
{
return 0;
}else
{
return 1;
}
}
I also have a club class that stores all info in an ArrayList<Player>
team.
my interface :
public interface Comparator<T>
{
int compare(T o1, T o2);
}
and I also have this class
public class ComparePlayers implements Comparator<Player>
{
public int compare(Player p1, Player p2)
{
if(p1.getGamesPlayed()< p2.getGamesPlayed())
{
return -1;
}else if(p1.getGamesPlayed()== p2.getGamesPlayed())
{
return p1.getLastName().compareTo(p2.getLastName());
}else
{
return 1;
}
}
the spec for this is:
When a new player is signed, she/he should be inserted into the Club class in alphabetical order of last name (and first name if last names are the same). To do this make your Person and Player class implement the appropriate Comparable interface.
Write a class ComparePlayers that implements the Comparator<Player>
interface. It should compare players by number of games played (and then by alphabetical order of surname if the number of games played is the same). Implement a new Constructor for the Club class that takes a Comparator<Player>
parameter. Hence write a main program that will print information about each player in the club where players are listed by decreasing order of games played. This should allow ordering to be dictated by the main program without modifying the code in any of your other classes.
I'm sorry if this is long winded but I am struggling to see what is wrong, I have tried several variation and it just wont work. this is due in on Friday, just a push in the right direction would be hugely appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将比较实现更改为:
Change your compare implementation to:
Person
和Player
不应实现Comparator
。如果它们需要按某种自然顺序进行比较,您应该实现Comparable
。您的
ComparePlayers
类看起来不错,有什么问题吗? (我假设您在这里使用java.util.Comparator
,不是吗?)为了对列表进行排序,您可以使用
Collections.sort(players, new ComparePlayers());< /code>,如果
players
的类型为List
(分别是该接口的实现)。请注意,如果
Person
和Player
应该Comparable
,那么您需要实现compareTo(Person p2)
并compareTo(Player p2)
其中 Player 的实现应调用super.compareTo(p2);
以实现同等游戏的情况。例子:
Person
andPlayer
shouldn't implementComparator
. If they need to be comparable by some natural order, you should implementComparable
.Your
ComparePlayers
class looks fine, what's the issue with it? (I assume youre usingjava.util.Comparator
here, don't you?)In order to sort the list you might use
Collections.sort(players, new ComparePlayers());
, ifplayers
is of typeList<Player>
(resp. an implementation of that interface).Note that if
Person
andPlayer
shouldComparable
, then you'd need to implementcompareTo(Person p2)
andcompareTo(Player p2)
where the implementation for Player should callsuper.compareTo(p2);
for the equal games played case.Example:
Comparator 的比较方法采用两个 Object 参数,而您的方法采用两个 Person 参数,因此编译器无法找到您重写此方法的位置。解决方案是将方法的参数更改为对象,或者(最好)使用通用比较器,因为这将允许您的比较方法具有 Person 参数,并且还会在编译时添加类型安全检查。
编辑 1:射击,您已经有了一个通用 Comparable 类的示例。
编辑2:在我读到有关此的评论之前,我没有看到您正在创建自己的界面。我同意评论者的观点——使用 java.util 中已有的类。
The compare method of Comparator takes two Object parameters while your method takes two Person parameters, and so the compiler can't find where you're overriding this method. The solution is to either change the parameters of the method to be Objects or else (and preferably) use a generic Comparator since this will allow your compare method to have Person parameters and also adds type safety checking at compile time.
edit 1: shoot, you already have an example of a generic Comparable class.
edit 2: I didn't see that you were creating your own interface til I read the comment about this. I agree with the commenter -- use the class already in java.util.
就像异常表明此声明
public intcompare(Person p1, Person p2)
必须更改为
public intcompare(Object p1, Object p2)
然后你也必须将方法代码调整为
Like the exception suggests this declaration
public int compare(Person p1, Person p2)
has to be changed to
public int compare(Object p1, Object p2)
Then you'll also have to adjust the method code as