Java为什么不需要运算符重载? [英] Why doesn't Java need Operator Overloading?

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

问题描述

为什么Java不需要运算符重载? Java有什么方法可以支持它吗?

Why doesn't Java need operator overloading? Is there any way it can be supported in Java?

推荐答案

Java仅允许对基本数字类型进行算术运算.这是喜忧参半的事情,因为尽管在其他类型(如复数,向量等)上定义运算符很方便,但始终存在与实现相关的特性.因此,运营商并不总是按照您的期望去做.通过避免运算符重载,可以更清楚地知道何时调用哪个函数.一个明智的设计在某些人的眼中动了.

Java only allows arithmetic operations on elementary numeric types. It's a mixed blessing, because although it's convenient to define operators on other types (like complex numbers, vectors etc), there are always implementation-dependent idiosyncrasies. So operators don't always do what you expect them to do. By avoiding operator overloading, it's more transparent which function is called when. A wise design move in some people's eyes.

这篇关于Java为什么不需要运算符重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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