使用 java Reflect 找出两个类是否属于同一类型(类、接口、枚举等)

发布于 2024-12-18 13:22:47 字数 83 浏览 3 评论 0原文

这是怎么回事?

我会保持简短和甜蜜。 如何确定两个类是否是同一类型。 我所说的类型是指它们是否都被定义为类、接口、枚举、注释或其他什么。

Whats up?

I'll keep it short and sweet.
How can I determine if two classes are the same type.
By type I mean if they are both defined as a Class, Interface, Enum, Annotion or whatever.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2024-12-25 13:22:47

了以下方法:

 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() 

java.lang.Class 上提供 挑选。

The following methods are available on 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() 

Take your pick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文