Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Các thuộc tính mới thêm vào @Deprecated annotation trong Java 9

Có hai tham số hoặc thuộc tính mới được thêm vào @Deprecated annotation trong Java 9. Đó là Since và forRemoval, cả hai đều tùy chọn với giá trị mặc định (default value) khi không chỉ định.

Since

Tham số chuỗi (string) này chỉ định phiên bản (version) mà API trở nên deprecated. Giá trị mặc định là chuỗi rỗng (empty string).

Cú pháp

@Deprecated(since="<version>")

forRemoval

Tham số boolean này chỉ định xem API có dự định bị xóa trong bản phát hành tương lai hay không. Giá trị mặc định là false khi không chỉ định.

Cú pháp

@Deprecated(forRemoval=<boolean>)

Ví dụ

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)");
    }
}

Kết quả

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