我按照这里的人的指示制作了两个课程,现在将不推荐使用的函数从一个类调用到另一个类(参见我以前的Question)。
我甚至将@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();
}
}
}
答案 0 :(得分:1)
注释@SuppressWarnings("deprecation")
附加到默认构造函数。
警告仅适用于编译时
您必须将@SuppressWarnings("deprecation")
指向方法main
本身指向类Ch10LU2Ex4
!
import java.util.Scanner;
import java.lang.annotation.*;
import java.lang.reflect.Method;
public class Ch10LU2Ex4 {
@SuppressWarnings("deprecation")
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();
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();
}
}
}
答案 1 :(得分:0)
如果您在main
方法前移动它会怎样?
@SuppressWarnings("deprecation")
public static void main(String[] args)