如何注释 Java 中类的弃用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15908887/
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
How to annotate deprecation of a class in Java?
提问by user1772643
I am going to deprecate a class in Java.
我将弃用 Java 中的一个类。
@Deprecated
class deprecatedClass
and I have list of this deprecated class,
我有这个弃用类的列表,
List<deprecatedClass> listOfDeperecatedClass
So do I need to add the @Deprecated
tag for this list too?
那么我是否也需要@Deprecated
为此列表添加标签?
Edit: @Deprecated
should have a capital 'D'.
See: http://docs.oracle.com/javase/7/docs/api/java/lang/Deprecated.html
编辑:@Deprecated
应该有一个大写的“D”。
请参阅:http: //docs.oracle.com/javase/7/docs/api/java/lang/Deprecated.html
回答by SQB
No, you don't need to. Adding the annotation @Deprecated
to DeprecatedClass
will generate a warning every time it's used.
不,你不需要。添加注释@Deprecated
到DeprecatedClass
将在每次使用时生成警告。
What you shoulddo however, is marking methods in otherclasses that take your deprecated class as an argument or return it, as deprecated as well. That goes for any access that other code may have to instances of your deprecated class — public fields, constants and so on. Those of course can't be used without an instance of your deprecated class, so the warning is given anyway, but in a correct deprecation annotation and comment, you should provide an explanation and point to an alternative, which is valuable information you needto give.
但是,您应该做的是将其他类中的方法标记为将您已弃用的类作为参数或将其返回为已弃用。这适用于其他代码可能对已弃用类的实例(公共字段、常量等)进行的任何访问。这些当然不能没有你不赞成使用的类的实例来使用,所以警告都会给出,但在一个正确的折旧注释和评论,你应该提供一个解释,并以点带替代,这是你有价值的信息需要到给。
A method signature is like a contract and so is a class signature. You're telling other programmers what methods they can call and how they can call them. You're telling them which fields are accessible. Other programmers base their code on that. If you really need to break that contract, you first need to provide a substitute for that contract (a new method with the same functionality), and tell them and give them time to switch to that new contract (deprecate the old methods and classes).
方法签名就像合同,类签名也是。您是在告诉其他程序员他们可以调用哪些方法以及如何调用它们。你告诉他们哪些字段是可访问的。其他程序员的代码基于此。如果你真的需要打破那个契约,你首先需要为那个契约提供一个替代品(一个具有相同功能的新方法),并告诉他们并给他们时间切换到那个新契约(弃用旧的方法和类) .
Of course, the above assumes that you're coding to an audience. If you're the only one using your code and you just want to deprecate to clean up your code without breaking the build, just deprecate the class, fix the warnings, and remove it.
当然,以上假设您正在为观众编码。如果您是唯一使用您的代码的人,并且您只想弃用以清理您的代码而不破坏构建,只需弃用该类,修复警告,然后将其删除。
回答by Keppil
Do you have operations on the List
as part of your public interface? In that case, mark all those methods as deprecated
too. Otherwise you should be fine.
您是否将操作List
作为公共接口的一部分?在这种情况下,将所有这些方法deprecated
也标记为。否则你应该没事。
回答by BlackBox
You only need to add the deprecation message to the declaration of anything you are deprecating. It serves as a warning that people should avoid an implementation which uses the deprecated class, such as in List<DeprecatedClass>
.
您只需要将弃用消息添加到您弃用的任何内容的声明中。它警告人们应该避免使用已弃用的类的实现,例如 in List<DeprecatedClass>
。
回答by Seid.M
Just marking as:
只需标记为:
@Deprecated
List<deprecatedClass> listOfDeperecatedClass
Should be okay.
应该没问题。