我按照这里的人对我的指示做了两个类,现在将不推荐使用的函数从一个类调用到另一个类(参见我以前的问题)。
我什至在函数调用上加上了@SuppressWarnings 行,但它仍然不起作用。我不明白为什么。我需要使用 @SuppressWarnings 注释来停止显示弃用警告。谁能告诉我把它放在哪里?
已弃用的类
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@interface number {
String arm();
}
public class armstrong {
@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");
}
}
}
使用已弃用的类
import java.util.Scanner;
import java.lang.annotation.*;
import java.lang.reflect.Method;
public class Ch10LU2Ex4 {
public static void main(String[] args) {
try {
System.out.println("Enter a number between 100 and 999:");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
@SuppressWarnings("deprecation")
armstrong obj = new armstrong();
obj.armStrong(x);
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();
}
}
}