Java 9 中的 @Deprecated 注释添加了哪些属性?

javaobject oriented programmingprogramming更新于 2025/7/14 22:04:17

Java 9 中的 @Deprecated 注释 添加了两个新参数或属性。这些参数是 Since 和 forRemoval,这两个参数都是可选的,当我们无法指定时,它们会有一个默认值。

Since

这个 string 参数指定了 API 被弃用的 版本。此元素的默认值为空 字符串。

语法

@Deprecated(since="<version>")

forRemoval

此布尔 参数指定是否打算在未来版本中删除该 API。当我们无法指定时,默认值为false 。

语法

@Deprecated(forRemoval=<boolean>)

示例

public class DeprecatedAnnotationTest {
   public static void main(String[] args) {
      DeprecatedAnnotationTest test = new DeprecatedAnnotationTest();
      test.method1();
      test.method2();
   }
   @Deprecated(since="7.0")
   public void method1() {
      System.out.println("@Deprecated(since=\"7.0\")");
   }
   @Deprecated(since="5.0", forRemoval=true)
   public void method2() {
      System.out.println("@Deprecated(since=\"5.0\", forRemoval=true)");
   }
}

输出

@Deprecated(since="7.0")
@Deprecated(since="5.0", forRemoval=true)

相关文章


有用资源