java - 如何检查类型化类的类型
问题描述
我在已声明为接受参数的方法中传递参数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");
应该执行
解决方案
该 if 语句永远不会起作用,因为您的对象是Class<?>
. 这将起作用:
if (o == Integer.class)
System.out.println("Integer")
推荐阅读
- typescript - 问:使用 Vue 3 和 typescript(使用 cli 构建的项目)使 vuex 存储在全球范围内可用 +已解决
- sqlite - Qt Quick如何在正确的时刻获取对象
- java - 在实际项目中使用 MTLS
- javascript - 禁用按钮,直到用户单击复选框 | JavaScript
- vue.js - 有没有办法在 vue.js 中使用换行符
- azure - 使用 Matillion 从 Azure 到 Snowflake 的增量负载
- javascript - 如何用javascript替换图像srcset
- python - RStudio 中的 Keras 模型设置(miniconda)
- assembly - MIPS 中 lui 之后的 ori 操作和 add 操作有什么区别?
- woocommerce - woocommerce:根据订购的产品自动为订单分配不同的自定义状态