Java generics and the Number class

后端 未结 5 1478
一个人的身影
一个人的身影 2021-01-11 16:16

I want to create a method that compares a number but can have an input that is any of the subclasses of Number.

I have looked at doing this in the following manner..

5条回答
  •  花落未央
    2021-01-11 16:51

    If it's really just about comparing the argument to another value of the same type parameter then you could do the following (just adding in T x for simplicity)

       public static > int evaluate(T inputNumber, T x) {
              if (inputNumber.compareTo(x) > 0) { ... }
       }
    

提交回复
热议问题