Java @deprecated vs @Deprecated
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20674135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@deprecated vs @Deprecated
提问by N K
I'm able to deprecate a function or class with the @Deprecated
annotation.
我可以使用@Deprecated
注释弃用函数或类。
But there is a @deprecated
javadoc tag in a javadoc comment itself marking the class/function as deprecated. Does the @deprecated
javadoc tag actually make the class/function deprecated?
但是@deprecated
在 javadoc 注释本身中有一个javadoc 标记,将类/函数标记为已弃用。@deprecated
javadoc 标签是否真的使类/函数被弃用?
采纳答案by Ben Barkay
@Deprecated
is an annotation that is read by the compiler, used to mark a method as deprecated to the compiler and will generate a deprecation compile-time warning if the method is used.
@Deprecated
是编译器读取的注释,用于将方法标记为编译器已弃用,如果使用该方法,将生成弃用编译时警告。
@deprecated
is a javadoc tag used to provide documentation about the deprecation. You can use it to explain why the method was deprecated and to suggest an alternative. It only makes sense to use this tag in conjunction to the @Deprecated annotation.
@deprecated
是一个 javadoc 标签,用于提供有关弃用的文档。您可以使用它来解释为什么不推荐使用该方法并建议替代方法。仅将此标记与 @Deprecated 注释结合使用才有意义。
Example usage:
用法示例:
/**
* This method does ...
* @deprecated As of <product> <version>, because ... use
* {@link #replacementMethod()} instead.
*/
@Deprecated
public void deprecatedMethod() {
// ...
}
Here is a guide on deprecation, check it out for more information.
这是弃用指南,查看更多信息。
To answer your question more specifically, you should use either @Deprecated
or both because it makes no sense that you would want this information to be restricted to the documentation while withholding it from the compiler. @Deprecated
marks your method as deprecated to any tool that cares about it (such as IDEs), as it is available at runtime and compile-time. Additionally, the javadoc tool does take notice of @Deprecated
even if you didn't add any information regarding the deprecation by using @deprecated
.
要更具体地回答您的问题,您应该使用其中一个@Deprecated
或两个,因为您希望将此信息限制在文档中而将其隐藏在编译器中是没有意义的。@Deprecated
将您的方法标记为已弃用任何关心它的工具(例如 IDE),因为它在运行时和编译时可用。此外,@Deprecated
即使您没有使用@deprecated
.
Merely documenting your methods as deprecated instead of annotating them means that users will have to manually search for usages instead of using tools such as the compiler or their own tools.
仅仅将您的方法记录为已弃用而不是注释它们意味着用户将不得不手动搜索用法而不是使用诸如编译器或他们自己的工具之类的工具。
回答by Nambi
@deprecated Javadoc Tag:You can use the @deprecated tag to make Javadoc show a program element as deprecated. The @deprecated tag must be followed by a space or newline.
@deprecated Javadoc 标记:您可以使用 @deprecated 标记使 Javadoc 将程序元素显示为已弃用。@deprecated 标签后必须跟一个空格或换行符。
@DeprecatedUsing the @Deprecated annotation to deprecate a class, method, or field ensures that all compilers will issue warnings when code uses that program element. In contrast, there is no guarantee that all compilers will always issue warnings based on the @deprecated Javadoc tag
@Deprecated使用 @Deprecated 批注弃用类、方法或字段可确保所有编译器在代码使用该程序元素时发出警告。相比之下,不能保证所有编译器总是根据 @deprecated Javadoc 标签发出警告
refer here
参考这里