多态性、转换为对象和类型擦除
Polymorphism, Casting to Object, and Type Erasure
这是我在 Whosebug 上的第一个 question/s,我希望你能帮助我解决我现在遇到的理解问题。
假设我正在这样做:
Object o = null;
String s = "Test";
System.out.println(o instanceof String); //This returns false
o = s;
System.out.println(o instanceof String); //This returns true
到目前为止一切顺利,但现在当我这样做时:
System.out.println( ((Object) o) instance of String) //Still prints true
那么,为什么即使在输出中我将 o 转换回对象 class,此语句仍输出 true?甚至
System.out.println(((Object)o).getClass())
打印 o 是 class 字符串。为什么会这样?
这引出了我的第二个问题,它与泛型和类型擦除有关。
我看了这样的代码示例:
private static <T> T cast(Object i){
return (T) i;
}
public static void main(String[] args){
Object o = null;
try{
o = main.<Double> cast("Test");
}catch(ClassCastException ex) {
System.out.println("Could not cast properly");
}
System.out.println(o == null); //Prints false
System.out.println(o instanceof String); //Prints true
System.out.println(o instanceof Double); //Prints false
}
根据我对类型擦除的理解,所有泛型 T 都将在运行时替换为 Object。当 运行 强制转换函数实际上应该发生的是
类似于 return (Object) i;
的内容。
正如我上面的第一个问题,为什么 System.out.println(o instanceof String);
打印为真? o 不应该是 Object 类型吗?
期待您的回答,谢谢:)!
So why does this statement print true even though inside the print I cast o back to an Object class?
您只是将引用转换为 Object
。实际对象还是String
类型的。并且 instanceof
检查是针对实际实例,而不是引用类型。
prints that o is of class String. Why so?
getClass()
方法 return Class
运行时对象类型的实例。
why does System.out.println(o instanceof String); print true?
如您所见,您将 String
类型作为参数传递给 cast()
方法。因此,即使引用类型是 Object
,实际对象的类型也是 String
。由于泛型,这里没有什么特别的事情发生。道理一样,结果也是一样。
这是我在 Whosebug 上的第一个 question/s,我希望你能帮助我解决我现在遇到的理解问题。
假设我正在这样做:
Object o = null;
String s = "Test";
System.out.println(o instanceof String); //This returns false
o = s;
System.out.println(o instanceof String); //This returns true
到目前为止一切顺利,但现在当我这样做时:
System.out.println( ((Object) o) instance of String) //Still prints true
那么,为什么即使在输出中我将 o 转换回对象 class,此语句仍输出 true?甚至
System.out.println(((Object)o).getClass())
打印 o 是 class 字符串。为什么会这样?
这引出了我的第二个问题,它与泛型和类型擦除有关。 我看了这样的代码示例:
private static <T> T cast(Object i){
return (T) i;
}
public static void main(String[] args){
Object o = null;
try{
o = main.<Double> cast("Test");
}catch(ClassCastException ex) {
System.out.println("Could not cast properly");
}
System.out.println(o == null); //Prints false
System.out.println(o instanceof String); //Prints true
System.out.println(o instanceof Double); //Prints false
}
根据我对类型擦除的理解,所有泛型 T 都将在运行时替换为 Object。当 运行 强制转换函数实际上应该发生的是
类似于 return (Object) i;
的内容。
正如我上面的第一个问题,为什么 System.out.println(o instanceof String);
打印为真? o 不应该是 Object 类型吗?
期待您的回答,谢谢:)!
So why does this statement print true even though inside the print I cast o back to an Object class?
您只是将引用转换为 Object
。实际对象还是String
类型的。并且 instanceof
检查是针对实际实例,而不是引用类型。
prints that o is of class String. Why so?
getClass()
方法 return Class
运行时对象类型的实例。
why does System.out.println(o instanceof String); print true?
如您所见,您将 String
类型作为参数传递给 cast()
方法。因此,即使引用类型是 Object
,实际对象的类型也是 String
。由于泛型,这里没有什么特别的事情发生。道理一样,结果也是一样。