计算机 · 2021年12月18日 0

Java Annotations

注解的作用:

  1. information for the compiler
  2. Compile-time and deployment-time processing
  3. Runtime processing

注解的格式

@Entity

如果注解只有一个元素,可以省略元素名称,如:

@SuppressWarnings(value = "unchecked")
void myMethod() { ... }

可以简写为:

@SuppressWarnings("unchecked")
void myMethod() { ... }

repeating annotation,类型相同的注解(since Java1.8):

@Author(name = "Jane Doe")
@Author(name = "John Smith")
class MyClass { ... }

可以使用预定义的注解或者自定义的注解
可以使用注解的地方:类/域/方法以及其他一些元素的声明。对于Java8而言,注解还可以用在类型上。

参考