重写抽象方法compare()

发布于 2024-11-04 06:10:20 字数 2099 浏览 0 评论 0原文

您好,我对编程相当陌生,我在比较方法方面遇到困难,我有几个类,我最初的问题是我的父类。

我收到此错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

路还长,别太狂 2024-11-11 06:10:20

将比较实现更改为:

public int compare(Object o1, Object 02)
{
   Person p1 = (Person)o1;
   Person p2 = (Person)o2;
   // 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());

}

Change your compare implementation to:

public int compare(Object o1, Object 02)
{
   Person p1 = (Person)o1;
   Person p2 = (Person)o2;
   // 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());

}
纵性 2024-11-11 06:10:20

PersonPlayer 不应实现 Comparator。如果它们需要按某种自然顺序进行比较,您应该实现Comparable

您的 ComparePlayers 类看起来不错,有什么问题吗? (我假设您在这里使用 java.util.Comparator ,不是吗?)

为了对列表进行排序,您可以使用 Collections.sort(players, new ComparePlayers());< /code>,如果 players 的类型为 List(分别是该接口的实现)。

请注意,如果 PersonPlayer 应该 Comparable,那么您需要实现 compareTo(Person p2)compareTo(Player p2) 其中 Player 的实现应调用 super.compareTo(p2); 以实现同等游戏的情况。

例子:

class Person<T extends Person> implements Comparable<T> {
  public int compareTo(Tp2) { ... }
}

class Player extends Person<Player> {
  public int compareTo(Player p2) {
    ...
    if(gamesPlayed == p2.getGamesPlayed() ) {
      return super.compareTo(p2);
    }
    ...
  }
}

Person and Player shouldn't implement Comparator. If they need to be comparable by some natural order, you should implement Comparable.

Your ComparePlayers class looks fine, what's the issue with it? (I assume youre using java.util.Comparator here, don't you?)

In order to sort the list you might use Collections.sort(players, new ComparePlayers());, if players is of type List<Player> (resp. an implementation of that interface).

Note that if Person and Player should Comparable, then you'd need to implement compareTo(Person p2) and compareTo(Player p2) where the implementation for Player should call super.compareTo(p2); for the equal games played case.

Example:

class Person<T extends Person> implements Comparable<T> {
  public int compareTo(Tp2) { ... }
}

class Player extends Person<Player> {
  public int compareTo(Player p2) {
    ...
    if(gamesPlayed == p2.getGamesPlayed() ) {
      return super.compareTo(p2);
    }
    ...
  }
}
油焖大侠 2024-11-11 06:10:20

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.

天生の放荡 2024-11-11 06:10:20

就像异常表明此声明

  • public intcompare(Person p1, Person p2)

必须更改为

  • public intcompare(Object p1, Object p2)

然后你也必须将方法代码调整为

public int compare(Object p1, Object p2)
{
   // if last names are the same compare first names
   if(((Person)p1).getLastName().equals(((Person)p2).getLastName()))
   {
       return ((Person)p1).getFirstName().compareTo(((Person)p2).getFirstName());
   }
   return ((Person)p1).getLastName().compareTo(((Person)p2).getLastName());

}

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

public int compare(Object p1, Object p2)
{
   // if last names are the same compare first names
   if(((Person)p1).getLastName().equals(((Person)p2).getLastName()))
   {
       return ((Person)p1).getFirstName().compareTo(((Person)p2).getFirstName());
   }
   return ((Person)p1).getLastName().compareTo(((Person)p2).getLastName());

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文