java.lang.Number doesn't implement “+” or any other operators?

后端 未结 7 1730
梦毁少年i
梦毁少年i 2020-12-17 19:56

I\'m creating a class which is supposed to be able to be used with an array of any type of number (float, int, etc), so here is one method I have:

// T exten         


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-17 20:28

    In Java, the arithmetic operators will only work on primitive types. The problem here is that Java has class representations of these primitive types, and the switch from one to another is generally implicit, via the feature known as autoboxing.

    In this case you will need to implement methods for the types of arithmetic operations you need, possibly having to create overloaded methods for every possible numeric type passed in for every operation.

提交回复
热议问题