前置知识: Java

枚举与注解

1 minIntermediate2026/6/14

Java枚举类型与注解系统

1. 枚举

public enum Status {
  ACTIVE("活跃"), INACTIVE("停用"), PENDING("待审核");

  private final String description;

  Status(String description) { this.description = description; }

  public String getDescription() { return description; }
}

2. 注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Cacheable {
  int ttl() default 3600;
  String key() default "";
}

@Cacheable(ttl = 600, key = "user:#id")
public User getUser(String id) { ... }