Java如何将方法标记为已弃用?

要将方法标记为deprecated,我们可以使用JavaDoc@deprecated注解。这就是我们从Java开始以来所做的。但是当Java语言引入新的元数据支持时,我们也可以使用注释。将方法标记为deprecated的注注解是@deprecated。

这两者之间的区别在于,将@deprecated放置在JavaDoc注释块中,而将@Deprecated放置为源代码元素。

package org.nhooo.example.annotation;

import java.util.Date;
import java.util.Calendar;

public class DeprecatedExample {
    public static void main(String[] args) {
        DeprecatedExample de = new DeprecatedExample();
        de.getDate();
        de.getMonthFromDate();
    }

    /**
     * Get current system date.
     *
     * @return current system date.
     * @deprecated This method will removed in the near future.
     */
    @Deprecated
    public Date getDate() {
        return new Date();
    }

    public int getMonthFromDate() {
        return Calendar.getInstance().get(Calendar.MONTH);
    }
}