简体   繁体   English

投射到不相关的接口时为什么要编译?

[英]Why does it compile when casting to an unrelated interface?

interface Printable {}
class BlackInk {}

public class Main {
    public static void main(String args[]) {
        Printable printable = null;
        BlackInk blackInk = new BlackInk();
        printable = (Printable)blackInk;
    }
}

If the preceding code is compiled and run, the result is a ClassCastException at printable = (Printable)blackInk; 如果前面的代码已编译并运行,则结果为ClassCastException,其位置为printable = (Printable)blackInk; . But, if Printable is changed to a class, it doesn't compile because blackInk can't be cast to Printable. 但是,如果将Printable更改为一个类,则因为blackInk无法转换为Printable而无法编译。 Why does it compile when Printable is an interface? 当Printable是接口时,为什么要编译?

The compiler does not know that this won't work: You could have a subclass of BlackInk that implements Printable. 编译器不知道这是行不通的:您可能拥有实现Printable的BlackInk子类。 Then the cast would be fine. 那演员就好了。

In situations where the compiler knows it won't work, you will get an error. 在编译器知道它不起作用的情况下,您会得到一个错误。

For example, if you make BlackInk final (so that there can be no subclasses) you get an error. 例如,如果将BlackInk final (这样就不能有子类),则会出现错误。

According to java language specification section: 5.5.1 Reference Type Casting : 根据Java语言规范部分:5.5.1 参考类型转换

For a compile-time reference type S (source) and a compile-type reference type T (target); 对于编译时引用类型S (源)和编译类型引用类型T (目标); While casting conversion from S to T , If S is a class Type 在将S转换为T如果Sclass Type

  • If T is a Class type : 如果T是一个Class类型

    1. Then either T is a subtype of S or S is a subtype of T . 然后要么T是的子类型S S是的子类型T Otherwise, a compile time error occurs. 否则,将发生编译时错误。
    2. if there exists a supertype X of T , and a supertype Y of S , such that both X and Y are provably distinct parameterized types, and that the erasures of X and Y are the same, a compile-time error occurs. 如果存在T的超类型XS的超类型Y ,从而XY都是可证明是截然不同的参数化类型,并且XY的擦除相同,则会发生编译时错误

       class S{} class T extends S{} //// S s = new S(); T t = (T)s; // run time ClassCastException will happen but no compile time error 
  • If T is an Interface type : 如果TInterface类型

    1. If S is not a final class, then, if there exists a supertype X of T , and a supertype Y of S , such that both X and Y are provably distinct parameterized types, and that the erasures of X and Y are the same, a compile-time error occurs. 如果S不是final类,那么,如果存在T的超类型XS的超类型Y ,使得XY都是可证明是截然不同的参数化类型,并且XY的删除相同,发生编译时错误 Otherwise, the cast is always legal at compile time ( because even if S does not implement T , a subclass of S might ) 否则,强制转换在编译时始终合法( 因为即使S不实现TS的子类也可能
    2. If S is a final class, then S must implement T, or a compile-time error occurs. 如果S是最终类,则S必须实现T,否则会发生编译时错误。

That is for your case, even if class casting is detected in compile time, Interface casting is detected in runtime . 这就是您的情况,即使在编译时检测到类强制转换,在runtime也检测到接口强制转换。

Type casting happens at run time (remember run time polymorphism). 类型转换在run time发生(记住运行时多态性)。 At compile time compiler doesn't see anything wrong with the code and compiles and at run time it tries to type cast blackink to printable and is unable to do so as blackink doesn't implement printable , hence the error. 编译时,编译器未发现代码和编译有任何问题,并且在运行时,它尝试将类型blackinkprintable blackink键入,并且由于blackink未实现printable ,因此无法这样做,从而导致错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么转换到不相关的接口会产生 ClassCastException 而不是编译时异常? - Why casting to unrelated interface produce ClassCastException instead of compile time exception? 将类转换为不相关的接口 - Casting a class to an unrelated interface 为什么编译器在调用一个不相关的接口类型时会选择这个带有类类型参数的泛型方法? - Why does the compiler choose this generic method with a class type parameter when invoked with an unrelated interface type? 为什么类编译为.class但接口不编译为.interface - Why classes compile to .class but interface does not to .interface 为什么“即时”转换在Java中给我一个编译错误? - Why does “on-the-fly” casting give me a compile error in Java? 为什么从超级接口转换为子接口有效? - Why does this casting from a super to a sub-interface work? 当枚举类型至少包含一个“扩展”枚举时,为什么将枚举转换为任何接口不会导致 Java 中的编译错误? - Why does casting an enum to any interface not cause a compilation error in Java when the enum type contains at least one "extended" enum? 为什么这个涉及通用接口方法的程序编译? - Why does this program involving a generic interface method compile? 为什么编译类型不匹配? 使用接口时扩展 - Why a compile type mismatch for ? extends when using an interface 为什么JAVA在接口中使用泛型类型时无法编译 - Why JAVA can not compile when using generic type in interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM