1

我按照这里的人对我的指示做了两个类,现在将不推荐使用的函数从一个类调用到另一个类(参见我以前的问题)。

我什至在函数调用上加上了@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();
        }
    }
}
4

2 回答 2

1

注释@SuppressWarnings("deprecation")附加到默认构造函数。

警告不会改变执行

警告仅适用于编译时

您必须将 指向Class@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();
        }
    }
}
于 2013-01-04T15:09:50.720 回答
0

如果你把它移到main方法前面怎么办?

@SuppressWarnings("deprecation")
public static void main(String[] args) 
于 2013-01-04T15:09:40.473 回答