[英]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
, 如果S
是class
Type
If T
is a Class
type : 如果T
是一个Class
类型 :
T
is a subtype of S
or S
is a subtype of T
. 然后要么T
是的子类型S
或 S
是的子类型T
。 Otherwise, a compile time error occurs. 否则,将发生编译时错误。 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
的超类型X
和S
的超类型Y
,从而X
和Y
都是可证明是截然不同的参数化类型,并且X
和Y
的擦除相同,则会发生编译时错误 。
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 : 如果T
是Interface
类型 :
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
的超类型X
和S
的超类型Y
,使得X
和Y
都是可证明是截然不同的参数化类型,并且X
和Y
的删除相同,发生编译时错误 。 Otherwise, the cast is always legal at compile time ( because even if S
does not implement T
, a subclass of S
might ) 否则,强制转换在编译时始终合法( 因为即使S
不实现T
, S
的子类也可能 ) 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. 编译时,编译器未发现代码和编译有任何问题,并且在运行时,它尝试将类型blackink
为printable
blackink
键入,并且由于blackink
未实现printable
,因此无法这样做,从而导致错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.