考虑java(或c#)中的以下示例:
//doesn't work
Object a = 5.2f;
int b = (int)a; //java.lang.ClassCastException: java.base/java.lang.Float cannot be cast to java.base/java.lang.Integer
//must first cast object to float, then can cast to integer
Object a = 5.2f;
int b = (int)(float)a;
我一直认为投射是可传递的(A=B & B=C THEN A=C
),但显然情况并非如此。
有人可以解释为什么这不起作用吗?