Using instanceof with a class Object [duplicate]
问题 This question already has answers here : Is there something like instanceOf(Class<?> c) in Java? (7 answers) Closed 4 years ago . What's the correct syntax to make this work? public boolean isTypeOf(Class type) { return this instanceof type; } I intend to call it with: foo.isTypeOf(MyClass.class); The method will be overriden, otherwise I would just use instanceof inplace. 回答1: Use Class.isInstance(obj): public boolean isTypeOf(Class type) { return type.isInstance(this); } This method