使用java反射查明两个类是否属于同一类型(Class,Interface,Enum等)

YLi*_*vay 1 java reflection enums interface class

这是怎么回事?

我会保持简短和甜蜜.如何确定两个类是否是同一类型.按类型我的意思是它们都被定义为Class,Interface,Enum,Annotion或其他.

Mat*_*ell 5

java.lang.Class上提供了以下方法:

 boolean    isAnnotation() 
          Returns true if this Class object represents an annotation type.
 boolean    isAnnotationPresent(Class<? extends Annotation> annotationClass) 
          Returns true if an annotation for the specified type is present on this element, else false.
 boolean    isAnonymousClass() 
          Returns true if and only if the underlying class is an anonymous class.
 boolean    isArray() 
          Determines if this Class object represents an array class.
 boolean    isAssignableFrom(Class<?> cls) 
          Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.
 boolean    isEnum() 
          Returns true if and only if this class was declared as an enum in the source code.
 boolean    isInstance(Object obj) 
          Determines if the specified Object is assignment-compatible with the object represented by this Class.
 boolean    isInterface() 
          Determines if the specified Class object represents an interface type.
 boolean    isLocalClass() 
          Returns true if and only if the underlying class is a local class.
 boolean    isMemberClass() 
          Returns true if and only if the underlying class is a member class.
 boolean    isPrimitive() 
          Determines if the specified Class object represents a primitive type.
 boolean    isSynthetic() 
Run Code Online (Sandbox Code Playgroud)

随便挑选.