简体   繁体   English

我如何在java中使用一个接受扩展接口类型的参数的函数来创建接口?

[英]How can i make an interface in java with a function that accepts a parameter of the type that extends the interface?

How can i make an interface in java with a function that accepts a parameter of the type that extends the interface?我如何在java中使用一个接受扩展接口类型的参数的函数来创建接口?

For example take the interface ISelfComparable以接口 ISelfComparable 为例

if class A extends it then i would expect it to implement如果 A 类扩展它,那么我希望它能够实现

bool compareTo(A other)

but if class B extends it then i would expect it to implement但如果 B 类扩展它,那么我希望它能够实现

bool compareTo(B other)

I know i can use a generic interface but it seems incorrect because it doesn't make any sense for A to extend ISelfComparable<B>我知道我可以使用通用接口,但它似乎不正确,因为 A 扩展 ISelfComparable<B> 没有任何意义

If this is impossible, what is the best practice in this kind of situation?如果这是不可能的,在这种情况下的最佳做法是什么?

The usual solution is self-bounded generics, as seen in the Enum class.通常的解决方案是自有界泛型,如Enum类中所示。

interface Example<T extends Example<T>> {
    void foo(T t);
}

public class ExampleImpl implements Example<ExampleImpl> {
    @Override
    public void foo(ExampleImpl example) {
    }
}

How it works is a bit dizzying, but is explained very well here for example.它的工作原理有点令人眼花缭乱,但例如在这里解释得很好。 There is also a very good answer on the subject here .也有关于这个问题的一个很好的答案在这里

Note that it isn't foolproof, as it allows this:请注意,它不是万无一失的,因为它允许:

public class ExampleImpl2 extends Example<ExampleImpl {
    @Override
    public void foo(ExampleImpl example) {

    }
}

But in practice the self-bounded idiom is used to express exactly the sort of thing you're after.但在实践中,self-bounded 成语用于准确表达您所追求的事物。

If you really, really, really need the parameter object to always be the exact same class as this , you have to do a runtime check.如果您真的,真的,真的需要参数对象始终与this完全相同,则必须进行运行时检查。 (It also raises the question of why you need this, but that would take us way off topic.) (它还提出了为什么你需要这个的问题,但这会让我们偏离主题。)

Have a look at the class java.lang.Comparable : it has an argument with the type of the objects that can be used int compareTo .看看类java.lang.Comparable :它有一个参数,可以使用 int compareTo的对象类型。

By analogy:类推:

public interface ISelfComparable<T extends ISelfComparable<T>> {
    boolean compareTo(T other);
}

There is a way to check the type of parameter but only in runtime.有一种方法可以检查参数的类型,但只能在运行时进行。 For example you can implement type checking in default method:例如,您可以在默认方法中实现类型检查:

interface ISelfComparable {
    default boolean compareTo(ISelfComparable param) {
        if (this.getClass() != param.getClass()) {
            throw new IllegalArgumentException();
        }
        ...
    }
}

Then each implementation of this interface should look like this:那么这个接口的每个实现应该是这样的:

class A implements ISelfComparable {
    @Override
    public  boolean compareTo(ISelfComparable param) {
        ISelfComparable.super.compareTo(param);
        ...
    }
}

In this case if you call new A().compareTo(new B());在这种情况下,如果您调用new A().compareTo(new B()); then java.lang.IllegalArgumentException will be thrown然后会抛出java.lang.IllegalArgumentException

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

相关问题 接口类型参数扩展接口? - Interface type parameter extends the interface? 如何制作接受任何类型变量的 Java 函数? - How can I make a Java function that accepts any type of variable? 如何实现扩展接口的类,该接口接受自身的参数? - How can I implement a class which extends an interface which accepts arguments of itself? Java如何返回扩展接口的泛型类型 - Java how to return a generic type that extends an interface Java GenericClass&lt; ? 扩展接口&gt;作为参数 - Java GenericClass< ? extends Interface> as a Parameter 如何添加对象 <? extends Interface> ? - How can I add an object to <? extends Interface>? 如何扩展2扩展相同但在java中使用不同的通用参数? - How to interface can extends 2 with that extends the same but with different generic parameters in java? 如何在Java中使用接口作为类型? - How can I use an interface as type in java? 如何在Java中使用泛型来创建一个泛型接口,该接口定义仅接受实现作为参数的方法? - How would I use generics in Java to create a generic interface that defines a method that only accepts the implementation as a parameter? Java:如何使用接口参数类型将变量传递给函数 - Java: How to pass a variable into function with interface parameter type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM