Java的@Deprecated 注解
@Deprecated注释告诉编译器一个方法、类或字段已被弃用,如果有人试图使用它,它应该生成一个警告。这就是不推荐使用的类或方法。它不再相关。停止使用它并不重要,因为它已被取代并且将来可能会被逐步淘汰。
不推荐使用的类或方法就是这样。它不再重要。事实上,它是如此不重要,以至于您不应再使用它,因为它已被取代并且将来可能不复存在。因为类的 API(应用程序编程接口)会随时间变化,所以Java提供了一种表达弃用的方法。为了一致性而重命名方法,添加了新的和更好的方法,并且字段发生了变化。然而,这样的变化带来了问题。如果您需要保留旧 API,直到开发人员转换到新 API,但不希望他们继续针对它进行编程,您可以使用内置注释来弃用该项目。
现在@Depricated注解的使用如下所示: 项目的 API 会随着时间的推移而发展。随着时间的推移,我们不希望人们再使用某些构造函数、字段、类型或方法。我们可以使用 @Deprecated 注释将这些元素标记为已弃用,而不是破坏项目的 API 的向后兼容性。 @Deprecated 向其他开发人员表明应避免使用标记元素。
在Java 9 中,对 @Deprecated 注释进行了两项新的增强:
- forRemoval :指示注释元素在未来版本中是否会被删除。默认值为假。
- 因为:返回注释元素被弃用的版本。默认值为空字符串。
如何弃用?
- 通过已弃用的界面
- 通过已弃用的类
- 通过弃用方法
- 通过弃用成员变量
- 通过弃用构造函数
我们使用@Deprecated注释来弃用一个方法、类或字段,并使用注释部分中的@deprecated Javadoc 标记来通知开发人员弃用的原因以及可以在其位置使用的内容。
1. 弃用的界面:
@Deprecated
interface GFG {
// Interface methods
}
2. 弃用的类
@Deprecated
class GFG {
// Class implementation
}
3. 弃用一个方法
class GFG {
@Deprecated
// old method
public void gfgmethod() {}
// new, alternate method
public void gfgmethod2() {}
}
4. 弃用成员变量
class GFG {
@Deprecated
public static final int MAX_SIZE = 1024;
// new, alternate field
public static final int MAX_UPLOAD_SIZE = 1024;
}
5. 弃用构造函数
class GFG {
@Deprecated
Gfg(String name, int length, int width) {
}
// new, alternate constructor
Gfg(Style style) {
}
}
实现:现在让我们用一个干净的Java程序来实现它们。
示例 1:使用已弃用的变量 name 。
Java
// Java Program Illustrating The @Deprecated Annotation
// Using deprecated variable name
// Main class
public class GFG {
// @deprecated number1 will be replaced by
// newnum field
@Deprecated
// Declaring and initializing integer variables
int number = 100;
// New field
final int newnumber = 100;
// Main
public static void main(String a[])
{
// Creating an object for the class
GFG obj = new GFG();
// Printing the number
System.out.println(obj.number);
}
}
Java
// Java Program Illustrating The @Deprecated Annotation
// Using deprecated method name
// Main class
public class GFG {
// @deprecated The function oldmethod will be replaced
// by new method
@Deprecated
// Method 1
// Old method
public void oldmethod()
{
// Print statement
System.out.println("This is a deprecated method");
}
// Method 2
// New method
public void newmethod(String m1)
{
// Print statement
System.out.println(m1);
}
// Method 3
// Main driver method
public static void main(String a[])
{
// Creating an object of class
GFG obj = new GFG();
// Now calling the old method
obj.oldmethod();
}
}
Java
// Java Program Illustrating The @Deprecated Annotation
// Using deprecated method name
// as well as the deprecated variable name
// Main class
class GFG {
// @deprecated
// The old function will be replaced by new one
@Deprecated
// Declaring and initializing integer values
int no = 10;
final int MAX_NUM = 100;
@Deprecated
// Method 1
// Old method
public void gfgMsg()
{
// Print statement
System.out.println("This method is deprecated");
}
// Method 2
// New Method
public void gfgMsg2(String msg, String msg2)
{
// Print statement
System.out.println(msg + msg2);
}
// Method 3
// Main driver method
public static void main(String a[])
{
// Creating an object of class
GFG obj = new GFG();
// Now calling the old method
obj.gfgMsg();
// Printing the num
System.out.println(obj.no);
}
}
Java
// Java Program Illustrating The @Deprecated Annotation
// Using deprecated constructor
// as well as the deprecated variable name.
// Main class
public class GFG {
// @deprecated
// The field number will be replaced by newnumber field
@Deprecated
int number = 10;
// new field
final static int newnumber = 10;
// @deprecated
// The constructor depexamplewill be replaced by second
// depexample
// Old constructor
GFG(int a, int b, int c)
{
// Print statement for old constructor
System.out.println(
"This is a deprecated constructor");
}
// new constructor
GFG(float d, int e, float f)
{
// Print statement for new constructor
System.out.println(d + f);
}
// Main driver method
public static void main(String a[])
{
// Creating object of class
GFG obj = new GFG(newnumber, newnumber, newnumber);
// Print and display the number
System.out.println(obj.number);
}
}
100
示例 2:使用已弃用的方法名称。
Java
// Java Program Illustrating The @Deprecated Annotation
// Using deprecated method name
// Main class
public class GFG {
// @deprecated The function oldmethod will be replaced
// by new method
@Deprecated
// Method 1
// Old method
public void oldmethod()
{
// Print statement
System.out.println("This is a deprecated method");
}
// Method 2
// New method
public void newmethod(String m1)
{
// Print statement
System.out.println(m1);
}
// Method 3
// Main driver method
public static void main(String a[])
{
// Creating an object of class
GFG obj = new GFG();
// Now calling the old method
obj.oldmethod();
}
}
This is a deprecated method
示例 3:使用不推荐使用的方法名称以及不推荐使用的变量名称。
Java
// Java Program Illustrating The @Deprecated Annotation
// Using deprecated method name
// as well as the deprecated variable name
// Main class
class GFG {
// @deprecated
// The old function will be replaced by new one
@Deprecated
// Declaring and initializing integer values
int no = 10;
final int MAX_NUM = 100;
@Deprecated
// Method 1
// Old method
public void gfgMsg()
{
// Print statement
System.out.println("This method is deprecated");
}
// Method 2
// New Method
public void gfgMsg2(String msg, String msg2)
{
// Print statement
System.out.println(msg + msg2);
}
// Method 3
// Main driver method
public static void main(String a[])
{
// Creating an object of class
GFG obj = new GFG();
// Now calling the old method
obj.gfgMsg();
// Printing the num
System.out.println(obj.no);
}
}
This method is deprecated
10
示例 4:使用不推荐使用的构造函数以及不推荐使用的变量名称。
Java
// Java Program Illustrating The @Deprecated Annotation
// Using deprecated constructor
// as well as the deprecated variable name.
// Main class
public class GFG {
// @deprecated
// The field number will be replaced by newnumber field
@Deprecated
int number = 10;
// new field
final static int newnumber = 10;
// @deprecated
// The constructor depexamplewill be replaced by second
// depexample
// Old constructor
GFG(int a, int b, int c)
{
// Print statement for old constructor
System.out.println(
"This is a deprecated constructor");
}
// new constructor
GFG(float d, int e, float f)
{
// Print statement for new constructor
System.out.println(d + f);
}
// Main driver method
public static void main(String a[])
{
// Creating object of class
GFG obj = new GFG(newnumber, newnumber, newnumber);
// Print and display the number
System.out.println(obj.number);
}
}
This is a deprecated constructor
10