Test if object is instanceof a parameter type

前端 未结 6 471
再見小時候
再見小時候 2020-12-04 21:02

Is there a way to determine if an object is an instance of a generic type?

public  test(Object obj) {
    if (obj instanceof T) {
        ...
    }
         


        
6条回答
  •  鱼传尺愫
    2020-12-04 21:22

    The only way you can do this check is if you have the Class object representing the type:

    Class type; //maybe passed into the method
    if ( type.isInstance(obj) ) {
       //...
    }
    

提交回复
热议问题