Implementing Incompatible Interfaces

This question already has an answer here:

  • interface and inheritance: “return type int is not compatible” 4 answers

  • I don't believe that's possible in this particular case. If both classes returned Object types you'd have some chance, but since you're mixing basic and object types, there's no compatible type that would support both interfaces.

    A different approach may be to implement appropriate interfaces that are compatible, then use composition to store an internal structure and map function calls to that as needed. That would assume that you don't need to satisfy or be usable as both interfaces, but rather that one in particular is the one you need to expose.

    However, if you need to make this class replaceable as two incompatible interfaces, it can't be done.


    You could make your own interface MyQueue with all the methods that Queue has minus the remove method and use that. You could give the MyQueue interface a Queue toQueue() method that returns the object converted into a queue.

    This conversion process could involve just returning a new instance of an anonymous Queue , which, for each method X, would simply call/return this. [X]. For the remove method you'd call this.remove() but then return a boolean rather than the return value of the this.remove() call.

    链接地址: http://www.djcxy.com/p/71990.html

    上一篇: 可以监视STM的争用级别吗?

    下一篇: 实现不兼容的接口