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")
添加回答
举报