接口Annotation指定方法
Class<? extends Annotation> annotationType()
Run Code Online (Sandbox Code Playgroud)
其中包含零信息1 javadoc.我不知道它有什么好处.所有我能找到的都是这个问题,但实际上并不需要它(因为在接受的一个下面的两个顶部答案显示).
它允许我们使用
a.annotationType().equals(MyAnnotation.class)
Run Code Online (Sandbox Code Playgroud)
但
a instanceof MyAnnotation
Run Code Online (Sandbox Code Playgroud)
做同样的工作......除非a是一个实现多个注释的类的实例 - 但是有没有人见过这样的野兽?
如果a是一个实例class A implements MyAnnotation, YourAnnotation,那么上面的两个测试不是等价的,但问题是哪一个更好......
(*)就像说getX()回报一样x.
这就像getClass(),除了由于在运行时实现注释的方式,getClass()经常为注释实例返回一个无意义的类型.
annotationType() 返回"正确的东西" - 注释的反射类型,而不是一些无意义的运行时类型.
比较Enum.getDeclaringClass(),返回枚举常量的"正确的东西",而myEnumConstant.getClass()可以返回枚举的合成子类型.
| 归档时间: |
|
| 查看次数: |
1354 次 |
| 最近记录: |