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");則應執行

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");則應執行

TA貢獻1868條經驗 獲得超4個贊
該 if 語句永遠不會起作用,因為您的對象是 的實例Class<?>
。這將起作用:
if (o == Integer.class) System.out.println("Integer")
添加回答
舉報