亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢查類型化類<?>的類型

如何檢查類型化類<?>的類型

繁花如伊 2023-08-09 16:25:02
我在已聲明為接受參數的方法中傳遞參數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");則應執行
查看完整描述

3 回答

?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

我在已聲明為接受參數的方法中傳遞參數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");則應執行


查看完整回答
反對 回復 2023-08-09
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

我在已聲明為接受參數的方法中傳遞參數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");則應執行


查看完整回答
反對 回復 2023-08-09
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

該 if 語句永遠不會起作用,因為您的對象是 的實例Class<?>。這將起作用:

if (o == Integer.class)
    System.out.println("Integer")


查看完整回答
反對 回復 2023-08-09
  • 3 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號