相关疑难解决方法(0)

接口中的Java转换

有人可以向我解释一下编译器在第一次投射时不会抱怨,但是在第二次投射中是否会抱怨?

interface I1 { }
interface I2 { }
class C1 implements I1 { }
class C2 implements I2 { }

public class Test{
     public static void main(){
        C1 o1 = new C1();
        C2 o2 = new C2();
        Integer o3 = new Integer(4);

        I2 x = (I2)o1; //compiler does not complain
        I2 y = (I2)o3; //compiler complains here !!
     }
}
Run Code Online (Sandbox Code Playgroud)

java casting interface

74
推荐指数
4
解决办法
2万
查看次数

标签 统计

casting ×1

interface ×1

java ×1