从实例到接口的转换被禁止?
非常简单的测试代码:
interface Base {
void interfaceTest();
static final String m = "1";
}
interface Child extends Base {
void interfaceTestChild();
}
class BaseClass implements Base {
@Override
public void interfaceTest() {
System.out.println("BaseClassInterfaceTest");
}
}
class ChildClass implements Child {
@Override
public void interfaceTest() {
System.out.println("ChildClassInterfaceTest");
}
@Override
public void interfaceTestChild() {
System.out.println("interfaceTestChild");
}
}
public class Src {
public Child testFunc() {
Base x = new BaseClass();
return (Child)x; <==Here got an "ClassCastException"
}
public static void main(String args[]) {
Src testSrcInstance = new Src();
testSrcInstance.testFunc().interfaceTest();
}
}
在 return (Child)x;
行中,我得到了一个“ClassCastException”,我对此感到非常困惑,因为 Child
extends Base
,因此 x
应该成功转换为 Child
。这种对话被一些android代码模仿:
EditText
的getText()
方法是:
public Editable getText() {
return (Editable) super.getText();
}
EditText
的超类是TextView
,其中getText()
方法为:
public CharSequence getText() {
return mText;
}
mText是一个CharSequence
,并且注意Editable扩展了CharSequence,所以可以看到,这些android代码投掷CharSequence
到 Editable
,就像我一样,将 Base
转换为 Child
,有什么区别吗?
Very simple code for test:
interface Base {
void interfaceTest();
static final String m = "1";
}
interface Child extends Base {
void interfaceTestChild();
}
class BaseClass implements Base {
@Override
public void interfaceTest() {
System.out.println("BaseClassInterfaceTest");
}
}
class ChildClass implements Child {
@Override
public void interfaceTest() {
System.out.println("ChildClassInterfaceTest");
}
@Override
public void interfaceTestChild() {
System.out.println("interfaceTestChild");
}
}
public class Src {
public Child testFunc() {
Base x = new BaseClass();
return (Child)x; <==Here got an "ClassCastException"
}
public static void main(String args[]) {
Src testSrcInstance = new Src();
testSrcInstance.testFunc().interfaceTest();
}
}
In the line return (Child)x;
I got a "ClassCastException" and I feel very confused about it, for Child
extends Base
, so x
should be converted to Child
successfully. this kind of conversation is imitated by some android codes:
the getText()
method of EditText
is:
public Editable getText() {
return (Editable) super.getText();
}
and the super class of EditText
is TextView
, of which the getText()
method is:
public CharSequence getText() {
return mText;
}
mText is a CharSequence
, and note that Editable extends CharSequence, so you can see, these android codes cast CharSequence
to Editable
, just as me, cast Base
to Child
, any difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,只有当
x
*实际上引用了实现Child
的某种类型的实例时,它才会起作用。在本例中,它没有 - 它仅引用BaseClass
的实例。这并没有为interfaceTestChild()
指定任何行为,那么如果您能够调用它,会发生什么?Java 只允许您转换为值实际支持的类型,即值所引用的对象的继承层次结构中的某种类型。
No, it will only work if
x
*actually refers to an instance of some type which implementsChild
. In this case, it doesn't - it only refers to an instance ofBaseClass
. That doesn't specify any behaviour forinterfaceTestChild()
, so what would expect to happen if you'd been able to call it?Java only lets you cast to a type which the value actually supports - i.e. some type in the inheritance hierarchy of the object that the value refers to.