Java-compareTo和运算符 [英] Java - compareTo and operators

查看:79
本文介绍了Java-compareTo和运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个实现Comparable的类Person(例如,将personA.heightpersonB.height比较),是否可以使用

If I have a class Person that implements Comparable (compares personA.height to personB.height, for example), is it possible to use

    personA < personB

代替

    personA.compareTo(personB) == -1? 

这样做是否有任何问题,或者我需要重载运算符吗?

Are there any issues in doing this or do I need to overload operators?

推荐答案

Java中没有运算符重载.您可能来自C ++背景.

There is no operator overloading in Java. You probably come from a C++ background.

您必须使用compareTo方法.

这篇关于Java-compareTo和运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆