Check if a Class Object is subclass of another Class Object in Java

前端 未结 8 1045
鱼传尺愫
鱼传尺愫 2020-11-28 20:33

I\'m playing around with Java\'s reflection API and trying to handle some fields. Now I\'m stuck with identifying the type of my fields. Strings are easy, just do myFi

8条回答
  •  清歌不尽
    2020-11-28 21:11

    Another option is instanceof:

    Object o =...
    if (o instanceof Number) {
      double d = ((Number)o).doubleValue(); //this cast is safe
    }
    

提交回复
热议问题