简体   繁体   English

Java中的多重继承:实现接口

[英]multiple inheritance in java : implementing interfaces

I have a class that implements Comparator but not that I need my class to be Serializable How can I implement both of them ? 我有一个实现Comparator的类,但我不需要我的类是可序列化的,如何实现这两个类呢?

public class A implements Comparator<A>
{
}

It's a common misconception that Java does not have multiple inheritance. 常见的误解是Java没有多重继承。 It doesn't have multiple inheritance of state, but it does have multiple inheritance of (declarations of) behavior, which is shown through interfaces. 它没有状态的多重继承,但确实具有行为的(声明)多重继承,这通过接口显示。 So you can have a single class implement multiple interfaces: 因此,您可以让一个类实现多个接口:

public class A implements Comparator<A>, Serializable {
}
import java.io.Serializable;
import java.util.Comparator;

public class A implements Comparator<A>, Serializable {

    @Override
    public int compare(A arg0, A arg1) {
        return 0;
    }
}

You can implement more than one interface, simply separate them with commas: 您可以实现多个接口,只需用逗号分隔即可:

class A implements Comparator<A>, Serializable {
}

You can extend only one superclass but you can implement as many interfaces as you like, eg 您只能extend一个超类,但是可以implement任意数量的接口,例如

public class A extends B implements Comparator<A>, Serializable {
}

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

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