1. 程式人生 > >如何判斷一個例項或者一個類實現某介面呢?

如何判斷一個例項或者一個類實現某介面呢?


For an instance

Character.Gorgon gor =newCharacter.Gorgon();

Then do

gor instanceofMonster

For a Class instance do

Class<?> clazz =Character.Gorgon.class;Monster.class.isAssignableFrom(clazz);



http://stackoverflow.com/questions/12145185/determine-if-a-class-implements-a-interface-in-java

You should use isAssignableFrom

:

if(YourInterface.class.isAssignableFrom(clazz)){...}