[英]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.