Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
考虑实现 Iterable 接口的方式让客户端使用 foreach 循环遍历类中的某些内容,有没有办法允许通过 <、> == 等进行对象比较?让 Comparable 接口允许这样做似乎几乎是显而易见的,但据我所知,事实并非如此。
不幸的是,这是不可能的,因为 Java 不支持运算符重载。
a<b翻译成是微不足道的a.compareTo(b)<0
a<b
a.compareTo(b)<0
但是没有运算符重载的选项
更不用说如何处理空值了