如何检查类型化类<?>的类型

我在已声明为接受参数的方法中传递参数type class<?>...其他,在传递类似参数后,String.class我Integer.class想知道在此方法中传递的参数的类型(类)。


我收到了什么参数,我将其转换为对象并尝试检查类型,但它不起作用。


public void processVarargIntegers(String label, Class<?>... others) {


    System.out.println(String.format("processing %s arguments for %s", others.length, label));

    Arrays.asList(others).forEach(a -> {


        try {

            Object o = a;

            if (o instanceof Integer) {

                System.out.println(" >>>>>>>>>>>>> Integer");

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    });

}

public void processVarargIntegers(String label, Class<?>... others) {


    System.out.println(String.format("processing %s arguments for %s", others.length, label));

    Arrays.asList(others).forEach(a -> {


        try {

            Object o = a;

            if (o instanceof Integer) {

                System.out.println(" Integer");

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    });

}

如果 a 是整数的实例,System.out.println(" Integer");则应执行


繁花如伊
浏览 115回答 3
3回答

动漫人物

我在已声明为接受参数的方法中传递参数type class<?>...其他,在传递类似参数后,String.class我Integer.class想知道在此方法中传递的参数的类型(类)。我收到了什么参数,我将其转换为对象并尝试检查类型,但它不起作用。public void processVarargIntegers(String label, Class<?>... others) {&nbsp; &nbsp; System.out.println(String.format("processing %s arguments for %s", others.length, label));&nbsp; &nbsp; Arrays.asList(others).forEach(a -> {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object o = a;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (o instanceof Integer) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" >>>>>>>>>>>>> Integer");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}public void processVarargIntegers(String label, Class<?>... others) {&nbsp; &nbsp; System.out.println(String.format("processing %s arguments for %s", others.length, label));&nbsp; &nbsp; Arrays.asList(others).forEach(a -> {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object o = a;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (o instanceof Integer) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" Integer");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}如果 a 是整数的实例,System.out.println(" Integer");则应执行

月关宝盒

我在已声明为接受参数的方法中传递参数type class<?>...其他,在传递类似参数后,String.class我Integer.class想知道在此方法中传递的参数的类型(类)。我收到了什么参数,我将其转换为对象并尝试检查类型,但它不起作用。public void processVarargIntegers(String label, Class<?>... others) {&nbsp; &nbsp; System.out.println(String.format("processing %s arguments for %s", others.length, label));&nbsp; &nbsp; Arrays.asList(others).forEach(a -> {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object o = a;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (o instanceof Integer) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" >>>>>>>>>>>>> Integer");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}public void processVarargIntegers(String label, Class<?>... others) {&nbsp; &nbsp; System.out.println(String.format("processing %s arguments for %s", others.length, label));&nbsp; &nbsp; Arrays.asList(others).forEach(a -> {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object o = a;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (o instanceof Integer) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" Integer");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}如果 a 是整数的实例,System.out.println(" Integer");则应执行

MYYA

该 if 语句永远不会起作用,因为您的对象是 的实例Class<?>。这将起作用:if&nbsp;(o&nbsp;==&nbsp;Integer.class) &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Integer")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java