如何在Java类中实现操作符 [英] How do I Implement Operators in a Java Class
问题描述
我正在尝试创建一个无符号整数类。
I am attempting to create a unsigned integer class.
public class UnsignedInteger extends Number implements Comparable<UnsignedInteger>
{
...
}
有没有办法实施运营商如; +, - ,*,/,<<,>>,|,^,>>>,<<
Is there a way to implement operators such as; +, -, *, /, <<, >>, |, ^, >>>, <<
推荐答案
Java不支持运算符重载。您唯一的选择是定义方法,如 add()
, subtract()
,乘以()
等,并在那里编写逻辑,并调用它们进行特定操作。
Java does not support Operator Overloading. The only option you have is define methods like add()
, subtract()
, multiply()
, etc, and write the logic there, and invoke them for particular operation.
你可以查看 BigInteger 类,了解如何定义方法支持各种业务。如果有兴趣,您甚至可以浏览源代码,您可以在 jdk 主目录的 src 文件夹中找到它。
You can have a look at BigInteger class to get an idea of how you can define methods to support various operations. And if interested, you can even go through the source code, that you can find in the src folder of your jdk home directory.
这篇关于如何在Java类中实现操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!