在界面中实现Comparable(implementing Comparable in an interface)

编程入门 行业动态 更新时间:2024-10-11 13:24:13
界面中实现Comparable(implementing Comparable in an interface)

我只使用其接口调用特定的类。 问题是,类本身实现了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.

更多推荐

本文发布于:2023-08-04 17:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:界面   Comparable   interface   implementing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!