K.T*_*ess 7 java casting interface
interface I {
}
class A {
}
class B {
}
public class Test {
public static void main(String args[]) {
A a = null;
B b = (B)a; // error: inconvertible types
I i = null;
B b1 = (B)i;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道为什么a不能被施展B,因为B不是从继承A.
我的问题是,为什么B b1 = (B)i;允许,因为B不是实现I?
为什么B b1 = (B)i;这一行不会强制运行时异常,因为它i是null?
Jef*_*rey 11
编译器不知道有关类层次结构的所有内容.就其而言,可能存在class B2 extends B implements I上述演员需要工作的情况.
| 归档时间: |
|
| 查看次数: |
168 次 |
| 最近记录: |