我最近开始阅读注释。我在这里弃用了armStrong()方法,我需要抑制弃用警告,但无论我把它放在哪里,都说“不必要的@SuppressWarnings(”弃用“)”。
任何人都可以告诉我在哪里放置它,以便不再发出method is deprecated
警告吗?
import java.io.*;
import java.lang.annotation.*;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface number
{
String arm();
}
public class Ch10LU2Ex4
{
@Deprecated
@number(arm = "Armstrong number")
public static void armStrong(int n)
{
int temp, x, sum = 0;
temp = n;
while(temp!=0)
{
x = temp%10;
sum = sum+x*x*x;
temp = temp/10;
}
if(sum==n)
{
System.out.println("It is an armstrong number");
}
else
{
System.out.println("It is not an armstrong number");
}
}
public static void main(String[] args)
{
try
{
Ch10LU2Ex4 obj = new Ch10LU2Ex4();
obj.invokeDeprecatedMethod();
Method method = obj.getClass().getMethod("armStrong", Integer.TYPE);
Annotation[] annos = method.getAnnotations();
for(int i = 0; i<annos.length; i++)
{
System.out.println(annos[i]);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
public void invokeDeprecatedMethod()
{
try
{
System.out.println("Enter a number between 100 and 999:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int x = Integer.parseInt(br.readLine());
Ch10LU2Ex4.armStrong(x);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
答案 0 :(得分:4)
使用已弃用的方法来自其他方法是导致警告的原因。
典型用法如下:
@SuppressWarnings("deprecation")
public void invokeDeprecatedMethod() {
instanceofotherclass.armStrong(1);
}
在同一个班级中,假设程序员知道他在做什么。
答案 1 :(得分:3)
这是a feature, not a bug。从该类本身中调用类的弃用方法不需要@SuppressWarnings
,因为此类调用首先不会生成弃用警告。从其他类调用已弃用的方法将需要@SuppressWarnings
注释。