【问题标题】:Java, how to inherit methods from abstract classJava,如何从抽象类继承方法
【发布时间】:2012-01-09 06:00:14
【问题描述】:

我有一个抽象类 Person 和接口可比较,它也用于程序的其他一些部分。目前我在 Person 中有一个 compareTo() 方法。当我尝试编译时,我得到:

The type Student must implement the inherited abstract method 
 Comparable<Person>.compareTo(Person, Person)

我到底要做什么?我不会在任何子类中实现此方法,因为我需要对所有子类都使用此方法,学生、导师、教授等......有没有更好的方法来做到这一点?

界面:

interface Comparable<Element> {
    public int compareTo(Element nodeA, Element nodeB);
}

抽象类人:

abstract class Person implements Comparable<Person> {

    protected String name;

    public Person(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String newName) {
        name = newName;
    }
    public String toString() {
        return name;
    }

    public int compareTo(Person personB) {
        int comp = this.name.compareTo(personB.getName());
        return comp;
    }
}

还有班级学生

class Student extends Person implements Comparable<Person> {

    private int id;

    public Student(String name, int id) {
        super(name);
        this.id = id;
    }

    public int getID() {
        return id;
    }

    public void setID(int newID) {
        id = newID;
    }

    public String toString() {
        return id + ", " + name;
    }
}

【问题讨论】:

    标签: java inheritance methods abstract


    【解决方案1】:

    从以下位置更改您的界面:

    interface Comparable<Element> 
    {     
       public int compareTo(Element nodeA, Element nodeB); 
    }
    

    到:

    interface Comparable<Element> 
    {     
       public int compareTo(Element nodeA); 
    }
    

    并使您的 Person 类定义为:

    abstract class Person implements Comparable<? extends Person> { /* ... */ }
    

    让你的学生(和其他人的子类成为):

    class Student extends Person { /* ... */ }
    

    就是这样。

    【讨论】:

      【解决方案2】:

      您的 Comparable 接口有一个方法 compareTo(Element nodeA, Element nodeB)。该方法在 Student 中没有定义,在 Person 中也没有定义。 Person 有以下方法:

      public int compareTo(Person personB)
      

      ,它不会覆盖compareTo(Person nodeA, Person nodeB)

      为什么要重新定义标准的java.util.Comparable 接口?

      【讨论】:

        【解决方案3】:

        您应该按照接口中显示的方法实现该方法,即使用两个参数

        public int compareTo(Person nodeA, Person nodeB)
        

        为避免以后出现此类问题,请使用@Override 注解:

        @Override
        public int compareTo(Person nodeA, Person nodeB)
        

        如果您尝试覆盖一个方法,但在其签名中出错,这将导致编译错误。

        另外,考虑使用 Java 的标准 Comparable

        【讨论】:

          【解决方案4】:

          您的Comparable&lt;Element&gt; 类声明了一个方法public int compareTo(Element nodeA, Element nodeB);,但在您的Person 类中,您实现了public int compareTo(Person personB),这不是同一个方法签名。

          您需要实现public int compareTo(Person personA, Person personB),或者将Comparable&lt;Element&gt; 类的方法定义更改为public int compareTo(Element other); 以覆盖核心Comparable 类的compareTo method

          此外,正如@murat 在下面的评论中提到的,使用@Override 注释会帮助您(假设您使用的是Java 1.5 或更高版本)。如果您将 @Override 添加到您实际上并未从超类覆盖的方法(例如您的两个参数 compareTo 方法),那么这将是编译器错误。

          【讨论】:

          • 这可能是最完整的正确答案了。此外,最好在您覆盖的方法之前添加@Override 标签,以确保它们与超类或接口中的某些方法匹配。
          • 无评论权限的用户评论 (Hound Dog):如果您使用 Eclipse 等 IDE,您可以轻松地从接口或抽象类生成方法(在 Eclipse 中使用 Source-&gt;Override/Implement Methods)和防止定义没有相同签名的新方法。
          【解决方案5】:

          你需要实现

          public int compareTo(Person nodeA, Person nodeB)
          

          在您的Person 课程中。目前你只有:

          public int compareTo(Person personB)
          

          【讨论】:

          • 他正在定义自己的 Comparable 接口,请参阅问题中的代码。
          • 抱歉相应地删除了我的评论。
          【解决方案6】:

          您需要在 Student 类中创建一个 compareTo 方法。

          【讨论】:

            【解决方案7】:
            interface Comparable<Element> {
                public int compareTo(Element nodeA, Element nodeB);
            }
            

            如果它是任何一个都有意义:

            interface Comparator<Element> { //comparator compares two instances of same type
                public int compare(Element nodeA, Element nodeB);
            }
            

            或者(对于你的情况必须是这样的):

            interface Comparable<Element> { //comparable compares itself with another instance of same type
                public int compareTo(Element that);
            }
            

            但是对于这两种情况,您都应该使用标准 Java 接口:即使您只使用Comparable,也请参阅Comparator

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2010-09-29
              • 2013-11-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-07-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多