23,405
社区成员
发帖
与我相关
我的任务
分享
public class MyTypeOf {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map.put("aa", 123);
aa(map);
}
public static void aa(Map<String,Object> map) {
System.err.println(map.get("aa") instanceof Integer);
System.err.println(map.get("aa") instanceof Object);
}
}
public class Test
{
public static void testInstanceof(Object x)
{
System.out.println("x instanceof Parent: "+(x instanceof Parent));
System.out.println("x instanceof Child: "+(x instanceof Child));
System.out.println("x getClass Parent: "+(x.getClass() == Parent.class));
System.out.println("x getClass Child: "+(x.getClass() == Child.class));
}
public static void main(String[] args) {
testInstanceof(new Parent());
System.out.println("---------------------------");
testInstanceof(new Child());
}
}
class Parent {
}
class Child extends Parent {
}
/*
输出:
x instanceof Parent: true
x instanceof Child: false
x getClass Parent: true
x getClass Child: false
---------------------------
x instanceof Parent: true
x instanceof Child: true
x getClass Parent: false
x getClass Child: true
*/