从实例到接口的转换被禁止?

发布于 2025-01-04 18:32:16 字数 1613 浏览 0 评论 0原文

非常简单的测试代码:

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代码模仿:

EditTextgetText()方法是:

public Editable getText() {
        return (Editable) super.getText();
    }

EditText的超类是TextView,其中getText()方法为:

public CharSequence getText() {
    return mText;
}

mText是一个CharSequence,并且注意Editable扩展了CharSequence,所以可以看到,这些android代码投掷CharSequenceEditable,就像我一样,将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听闻余生 2025-01-11 18:32:16

在行中 return (Child)x;我得到了一个“ClassCastException”,我对此感到非常困惑,因为 Child 扩展了 Base,所以 x 应该成功转换为 Child 。

不,只有当 x *实际上引用了实现 Child 的某种类型的实例时,它才会起作用。在本例中,它没有 - 它仅引用 BaseClass 的实例。这并没有为 interfaceTestChild() 指定任何行为,那么如果您能够调用它,会发生什么?

Base x = new BaseClass();
// Imagine this had worked...
Child child = (Child)x;
// What would this do? There's no implementation!
child.interfaceTestChild();

Java 只允许您转换为值实际支持的类型,即值所引用的对象的继承层次结构中的某种类型。

in the line return (Child)x; i got a "ClassCastException", i feel very confused about it, for Child extends Base, so x should be converted to Child successfully.

No, it will only work if x *actually refers to an instance of some type which implements Child. In this case, it doesn't - it only refers to an instance of BaseClass. That doesn't specify any behaviour for interfaceTestChild(), so what would expect to happen if you'd been able to call it?

Base x = new BaseClass();
// Imagine this had worked...
Child child = (Child)x;
// What would this do? There's no implementation!
child.interfaceTestChild();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文