下转型错误(Java)。以下代码似乎不起作用,欢迎解释。

huangapple 未分类评论46阅读模式
英文:

Down casting error (Java). The following code seems to not work, any explanation is appreciated

问题

我最近阅读了关于Java中的向上转型和向下转型的内容。我尝试了以下代码,但似乎无法正常工作。

class Super {
    void print() {
        System.out.println("Super");
    }
}

class Sub extends Super {
    void print() {
        System.out.println("Sub");
    }
}

class Program {
    public static void main(String[] args) {
        Super obj = new Super();
        obj.print();
        Sub obj1 = (Sub) obj;
        obj1.print();
    }
}

任何帮助都将不胜感激。

英文:

I recently read up on up casting and down casting and down casting in Java. I tried the following code but it seems to not work.

class Super {
    void print() {
        System.out.println("Super");
    }
}

class Sub extends Super {
    void print() {
        System.out.println("Sub");
    }
}

class Program {
    public static void main(String[] args) {
        Super obj = new Super();
        obj.print();
        Sub obj1 = (Sub) obj;
        obj1.print();
    }
}

Any help is appreciated.

答案1

得分: 0

如果我们以正方形和长方形的例子来说明,我们知道正方形是长方形,但长方形不是正方形。因为通常情况下,向下转型会丢失信息,JVM 显然不希望发生这种情况,所以它会导致您的程序在运行时失败。

英文:

If we take the example of square and rectangle, we know that a square is a rectangle, but a rectangle is not a square. Because in general, casting down loses information, the JVM clearly does not want that to happen, so it causes your program to fail in the Runtime.

答案2

得分: 0

Subclass可以执行与SuperClass相同的操作,反之亦然。SubClass可以为自身实现一些方法,因此Subclass并不是SuperClass,如果调用强制转换运算符,将会抛出ClassCastException

英文:

Subclass can do anything that SuperClass can do, vice versa SubClass can implement some methods for its own, thus Subclass is not SuperClass,ClassCastException will be thrown if you invoke casting operator.

答案3

得分: 0

让我们以一个通用示例来说明,您可以在任何地方应用它。

A b = (C)d // 这里 A 和 C 是类,b 和 d 是引用

现在在编译时,编译器会检查以下内容:

  • 类型 AC 是否相同
  • C 是否是 A 的子类

如果其中任何一个条件不满足,您将会得到编译时错误 "inconvertible types"。

在您的情况下,第一个条件为真,因此您的代码可以编译通过。

然后在运行时,JVM 会检查以下内容:

  • d 的底层类型是否相同
  • d 是否是 C 的子类

在您的情况下,第二个条件不满足,所以您会得到 ClassCastException

您所提到的向下转型可以发生在原始类型的情况下,这被称为 "缩小原始类型"。

英文:

Let's take a general example , you can apply it anywhere

A b = (C)d // here A and C are classes and b and d are references 

now at compile time the compiler checks
is the type of A and C are the same is C is child of A if any of two condition fails you get compile time error inconvertable types

In your case the first condition is true so your code compiles fine.

Now at runtime the JVM checks is underlying type of d is same or is d is child of C in your case the second condition fails so you get ClassCastExceptoin

The downcast you're taking about can happen in case of primitives "it is called narrowing the primitive"

huangapple
  • 本文由 发表于 2020年4月10日 11:03:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/61133403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定