Check if a object is a instance of a class (but not a instance of its subclass)

前端 未结 7 1516
天命终不由人
天命终不由人 2020-12-03 04:08

For this example:

public class Foo{}

public class Bar extends Foo{}

....

void myMethod(Foo qux){
   if (checkInstance(qux,Foo.class)){
     ....
   }
}
         


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 04:49

    If you are looking for exact class match the only means is qux.getClass().equals(Foo.class). instanceof will also return true for subclasses.

提交回复
热议问题