我只使用其接口调用特定的类。 问题是,类本身实现了Comparable,但因为我通过不同的接口引用类,编译器不知道它实现了Comparable。 我确信有一个简单的解决方案...但我现在无法想到它。
I am calling a specific class using only its interface. The problem is, the class itself implements Comparable, but because I am referring to the class via a different interface, the compiler does not know it implements Comparable. I'm sure there is an easy solution to this... but I just can't think of it right now.
最满意答案
实现接口的所有东西是否都会实现Comparable<T> ? 如果是这样,我建议你让接口扩展Comparable<T> 。
否则,如果您碰巧知道在这种情况下它可以工作,那么您可以将其转换为Comparable<T> 。 当然,这会丧失一些编译时类型的安全性,但这就是野兽的本质。
Will everything that implements the interface also implement Comparable<T>? If so, I suggest you just make the interface extend Comparable<T>.
Otherwise, you could just cast to Comparable<T> if you happen to know that in this case it will work. Of course, that loses some compile-time type safety, but that's the nature of the beast.
更多推荐
发布评论