【发布时间】:2017-11-14 16:58:44
【问题描述】:
我对这个(可能)简单的转换示例有一个尴尬的问题。你能帮帮我吗?
public class Example1 {
interface ParentIf{}
interface ChildIf extends ParentIf {}
interface OtherIf {}
class ParentCl {}
class ChildCl extends ParentCl {}
class OtherCl {}
public static void main(String[] args) {
ChildIf cI = null;
ParentIf pI = null;
OtherIf oI = null;
ChildCl cC = null;
ParentCl pC = null;
OtherCl oC = null;
cI = (ChildIf)oI; //case1 - fine
cC = (ChildCl)oC; //case2 - inconvertible types
cI = (ChildIf)oC; //case3 - fine
}
}
但更尴尬的是我不知道为什么其他两个语句都可以。
我看不到 OtherIf 和 ChildIf 之间的任何联系。那么在case1中这两个接口之间没有“扩展”时,如何将OtherIf转换为ChildIf?
【问题讨论】: