当java可以隐式地进行转换时,为什么不隐式地进行转换?请用一些简单的例子来解释一下?
The point is that upcasting will always succeed, so it's safe - whereas downcasting can fail:
String x = getStringFromSomewhere();
Object y = x; // This will *always* work
But:
Object x = getObjectFromSomewhere();
String y = (String) x; // This might fail with an exception
Because it's a "dangerous" operation, the language forces you to do it explicitly - you're basically saying to the compiler "I know more than you do at this point!"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.