我想我有这个界面:
public interface MyInterface {
void doStuff();
}
Run Code Online (Sandbox Code Playgroud)
具体实施:
public class HardCoreConcrete implements MyInterface {
void doStuff() {
// i really do stuff, honest
}
}
Run Code Online (Sandbox Code Playgroud)
假设我有这个注释:
@Target(ElementType.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
Class<MyInterface> clazz;
}
Run Code Online (Sandbox Code Playgroud)
它会像这样使用:
@MyAnnotation(clazz = HardCoreConcrete.class)
public class SomeOtherClass {
...
}
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?我的编译器抱怨,对于clazz,它期望类型MyInterface!但是HardCoreConcrete实现了MyInterface.
难道我做错了什么?这是不允许的?我运气不好吗?
Thi*_*ilo 25
你需要
public @interface MyAnnotation {
Class<? extends MyInterface> clazz;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9984 次 |
| 最近记录: |