简体   繁体   English

Java,如何从抽象类继承方法

[英]Java, how to inherit methods from abstract class

I have an abstract class Person and and interface comparable, which is also used for some other part of the program. 我有一个抽象类Person和接口可比,它也用于程序的其他部分。 Currently I have a method compareTo() in Person. 目前我在Person中有一个方法compareTo()。 When I try to compile, I get : 当我尝试编译时,我得到:

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

What exactly do I have to do? 我到底要做什么? I don't wont to implement this method in any of the subclasses, because I need this method for all of them, Student, Tutor, Professor, etc... Is there a better way of doing this? 我不会在任何子类中实现此方法,因为我需要所有这些方法,学生,导师,教授等...有更好的方法吗?

Interface: 接口:

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

Abstract class Person: 抽象类人物:

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;
    }
}

And class Student 和班级学生

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;
    }
}

Your Comparable<Element> class declares a method public int compareTo(Element nodeA, Element nodeB); 您的Comparable<Element>类声明了一个方法public int compareTo(Element nodeA, Element nodeB); , but in your Person class, you implement public int compareTo(Person personB) , which is not the same method signature. ,但是在Person类中,实现了public int compareTo(Person personB) ,它与方法签名不同。

You need to either implement public int compareTo(Person personA, Person personB) , or alter your Comparable<Element> class's method definition to be public int compareTo(Element other); 你需要实现public int compareTo(Person personA, Person personB) ,或者将你的Comparable<Element>类的方法定义改为public int compareTo(Element other); to override the core Comparable class's compareTo method . 覆盖核心Comparable类的compareTo方法

Also, as @murat mentions below in the comment, using the @Override annotation would help you out (assuming you're on Java version 1.5 or higher). 另外,正如@murat在评论中提到的那样,使用@Override注释会帮助你(假设你使用的是Java 1.5或更高版本)。 If you add @Override to a method that you're not actually overriding from a superclass (such as your two-argument compareTo method), then it will be a compiler error. 如果将@Override添加到一个实际上没有覆盖超类的方法(例如你的双参数compareTo方法),那么它将是编译器错误。

Your Comparable interface has a method compareTo(Element nodeA, Element nodeB) . 您的Comparable接口有一个方法compareTo(Element nodeA, Element nodeB) This method is not defined in Student, and it's not defined in Person either. 此方法未在Student中定义,也未在Person中定义。 Person has the following method: 人有以下方法:

public int compareTo(Person personB)

, which doesn't override compareTo(Person nodeA, Person nodeB) ,它不会覆盖compareTo(Person nodeA, Person nodeB)

Why are you redefining the standard java.util.Comparable interface? 为什么要重新定义标准的java.util.Comparable接口?

You should implement the method as it appears in the interface, ie with two arguments 您应该实现接口中出现的方法,即使用两个参数

public int compareTo(Person nodeA, Person nodeB)

To avoid such problems in the future use the @Override annotation: 为避免将来出现此类问题,请使用@Override注释:

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

This will cause a compilation error if you try to override a method, but make a mistake in its signature. 如果您尝试覆盖方法,但在签名中出错,则会导致编译错误。

Also, consider using Java's standard Comparable . 另外,请考虑使用Java的标准Comparable

Change your interface from: 更改您的界面:

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

to: 至:

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

And make your Person class be defined as: 并将您的Person类定义为:

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

And make your Student (and other Person-subclasses be): 并让你的学生(和其他人 - 子类):

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

That is all. 就这些。

You need to implement 你需要实施

public int compareTo(Person nodeA, Person nodeB)

In your Person class. 在你的Person类中。 Currently you only have: 目前你只有:

public int compareTo(Person personB)

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

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

It makes sense if it was either: 它是否有意义,如果它是:

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

or (has to be something like this for your case): 或者(对于你的情况,必须是这样的):

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

But for both cases, you should use the standard Java interfaces: Even though you are only using Comparable also see Comparator . 但对于这两种情况,您应该使用标准的Java接口:即使您只使用Comparable也请参阅Comparator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM