How do I Implement Operators in a Java Class

后端 未结 7 1818
南旧
南旧 2020-12-07 01:30

I am attempting to create a unsigned integer class.

public class UnsignedInteger extends Number implements Comparable 
    { 
    ...          


        
7条回答
  •  失恋的感觉
    2020-12-07 02:13

    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.

    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.

提交回复
热议问题