【问题标题】:Java: Casting ParentClass and ChildClass (Downcast Runtime Error)Java:强制转换 ParentClass 和 ChildClass(向下转换运行时错误)
【发布时间】:2013-01-19 07:20:12
【问题描述】:
public class InheritanceDemo {

    public static void main(String[] args) {

        ParentClass p = new ParentClass();
        ChildClass c = new ChildClass();

        //Casting ChildClass to ParentClass
        ParentClass pc = new ChildClass();
        pc.parentClassMethod(); //Output: Parent Class Method (as expected)

        //Again Casting Parent Class to ChildClass explictly
        //Question 1 for this code
        ChildClass cp = (ChildClass) pc;
        cp.parentClassMethod(); //Output: Parent Class Method (unexpected)

        ChildClass cc1 = (ChildClass) new ParentClass();
        cc1.parentClassMethod(); //Compiles, but Run Time Error

        ChildClass cc2 = (ChildClass) p;
        cc2.parentClassMethod(); //Compiles, but Run Time Error

    }
}

class ParentClass {

    public void parentClassMethod(){
        System.out.println("Parent Class Method");
    }

}

class ChildClass extends ParentClass {

    public void ParentClassMethod(){
        System.out.println("Parent Class Method From Child Class");
    }

    public void ChildClassMethod(){
        System.out.println("Child Class Method");
    }

}

问题1:

现在,我在 ParentClassChildClass 类(覆盖)中都有一个名为 parentClassMethod 的方法。当我将ParentClass转换为ChildClass然后调用parentClassMethod时,如果cp引用ChildClass,为什么它执行ParentClass方法而不是ChildClass的方法?

问题2:

(i)ChildClass cp = (ChildClass) pc;

(ii)ChildClass cc1 = (ChildClass) new ParentClass(); (iii)ChildClass cc2 = (ChildClass) p;

如果 (i) 工作正常,为什么不是 (ii) 或 (iii)?

因为在这两种情况下我都是从ParentClass 转换为ChildClass

【问题讨论】:

    标签: java casting type-conversion downcast


    【解决方案1】:

    现在,我在 ParentClass 和 ChildClass 类中都有一个名为 parentClassMethod 的方法(覆盖)。

    不,你没有。在ParentClass 中有一个名为parentClassMethod 的方法,在ChildClass 中有一个名为ParentClassMethod 的方法。由于所有 Java 标识符都区分大小写,因此两者之间没有关联。 ParentClassMethod 不会从 ParentClass 覆盖 parentClassMethod

    如果 (i) 工作正常,为什么不是 (ii) 或 (iii)?

    在 (ii) 和 (iii) 中,您尝试将 ParentClass 的实例转换为 ChildClass 的实例。这是不允许的,因为 ChildClass is-not-a ParentClass,就像 ObjectString 一样。

    在 (i) 中,您尝试将 ChildClass 的实例(存储在声明为 ParentClass 的引用中)转换为 ChildClass,这是允许的。

    在进行强制转换时,重要的是运行时类型(换句话说,new T() 中使用了 T 的内容)。

    【讨论】:

    • 这就是为什么你应该总是使用@Override注解。如果你把它放在ChildClass#ParentClassMethod() 你会得到一个错误告诉你,实际上(最好的曼迪帕廷金/伊尼戈蒙托亚公主新娘口音)“我认为该方法不会覆盖你认为它覆盖的东西”。
    • 谢谢马克,回复。这是我在程序中输入 ParentClassMethod 而不是 parentClassMethod 的错误。现在,我可以看出区别了。它有效!!!
    • 当我将 ParentClass 对象转换为 ChildClass 对象时,我可以访问 ParentClass 和 ChildClass 中的方法。但是,当我将 ChildClass 对象转换为 ParentClass 对象时,如何允许访问这些方法?即我只需要被允许访问 ParentClass 中的方法吗?请说清楚?谢谢。
    • 当我将 ParentClass 对象转换为 ChildClass 对象时,我可以访问 ParentClass 和 ChildClass 中的方法。ParentClass 的实例转换为 ChildClass 将始终失败,所以它允许你使用什么方法是无关紧要的。如果您的意思是您在声明为ParentClass变量 中有一个ChildClass,并且您将该变量 转换为ChildClass,那么这是正确的。如果将ChildClass 放入ParentClass 类型的变量中,则只能看到ParentClass 中的方法,但在运行时仍会调用最具体的方法(ChildClass)。
    【解决方案2】:

    当泛型不存在时,让我们回到 jdk 1.4。

      Vector test = new Vector();
       test.add("java");
    

    现在在检索你所做的事情时:

    String str = (String) test.get(0);
    

    所以 string 最初是作为一个对象存储在 vector 中的,然后你将它向下转换为 String 并且一切正常。

    但是如果你尝试跟随,它不会起作用:

    String str = (Object) new Object();
    

    所以您可以看到,在第一种情况下,它是 String,但引用是 Object [inside vector],在这种情况下,您可以向下转换,它会起作用。

    要回答您的第一个问题,java 区分大小写并且您的方法名称的大小写不匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 2012-08-25
      • 2011-05-28
      • 1970-01-01
      • 2014-10-19
      • 2011-02-12
      • 2012-08-07
      相关资源
      最近更新 更多