简体   繁体   English

两个通用接口相互需求,如何正确实现它们?

[英]Two Generic Interfaces require each other, how to proper implement them?

I want to use 2 Implementations, but I'm not sure how I should pass each Implementation to each of them. 我想使用2个实现,但是我不确定如何将每个实现传递给它们。 This is what I've got so far: 到目前为止,这是我得到的:

Interface of A: A接口:

public interface A<T extends A<T, U>, U extends B<T, U>>{
}

Interface of B: B接口:

public interface B<T extends A<T, U>, U extends B<T, U>> {
}

Implementation of A: A的实现:

public class AImpl<T extends B<AImpl<T>, T>> implements A<AImpl<T>, T>{
}

Implementation of B: B的实现:

public class BImpl<T extends A<T, BImpl<T>>> implements B<T, BImpl<T>> {
}

Now i want to use A and B. I'm not really sure how to implement them. 现在我想使用A和B。我不太确定如何实现它们。 Here is my initial attempt: 这是我最初的尝试:

Main: 主要:

//this doesn't work
private AImpl<BImpl> a; 
private BImpl<AImpl> b; 

public main() {
    a = new AImpl<BImpl>();
    b = new BImpl<AImpl>();
}

Inheritance can be far less useful for an interface than a class. 继承对接口的作用远不如类。

SUGGESTION: Just refactor and simplify your interfaces so you don't have to extend anything but the class itself! 建议:就重构和简化接口,所以您不必扩展任何内容,但类本身!

Fewer, smaller interfaces; 界面更少,更小; and shallow inheritance hierarchies for interfaces, are generally a Good Thing. 接口的浅继承层次结构通常是一件好事。 IMHO... 恕我直言...

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

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