简体   繁体   English

Java Generics 和注解:类型安全

[英]Java Generics and Annotations: Type-Safe

I know that I can do something like:我知道我可以做类似的事情:

public class AbstractDao<T extends Bean> {

}

In this case, Bean is an class or interface.在这种情况下,Bean 是一个 class 或接口。

I was wondering if is there a way to do this with annotations, like:我想知道是否有办法用注释来做到这一点,比如:

public class AbstractDao<T extends javax.persistence.Entity> {

}

Is something like this possible?这样的事情可能吗?

Thanks in advance!提前致谢!

Unfortunately, there is no inheritance in annotations, so it's not possible.不幸的是,注释中没有inheritance,所以这是不可能的。

Have a look at the answers to the similar question here:在这里查看类似问题的答案:

Why is not possible to extend annotations in Java? 为什么无法在 Java 中扩展注释?

No. The Java Language Specification says that type variables can only be constrained to extend / implement particular classes or interfaces.不可以。 Java 语言规范类型变量只能被限制为扩展/实现特定的类或接口。 Since annotating a class does not cause the class to extend or implement a particular type, you can't constrain by annotation.由于注释 class 不会导致 class 扩展或实现特定类型,因此您不能通过注释进行约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM