Java 中的类转换异常

发布于 2024-12-29 16:00:17 字数 394 浏览 1 评论 0原文

我在相互转换类时遇到问题。

要更详细地解释它,请看一下这张图片。

The Image

在我的代码中,我执行 SkillBase s = new SkillBase(); 然后设置该类中的一些值。 然后我尝试执行 Subclass sub = (Subclass)s; 但运行时它给出了 ClassCastException。

我添加了一个小的调试部分,它检查 instanceof 是否返回 true。

我尝试过谷歌,也在这里看到了一些问题并阅读了它们(他们都没有适合我的答案)

那么,我该怎么办?

I'm having a problem with casting Classes to each other.

To explain it in more detail, take a look at this image.

The Image

In my code, I do SkillBase s = new SkillBase(); and then set some values in that class.
Then I try to do Subclass sub = (Subclass)s; but when running it it gives a ClassCastException.

I added a small debug part, which checks if it's instanceof which returns true.

I've tried the google, saw some questions on here aswell and read them (none of them had an answer that was for me)

So, what do I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

再见回来 2025-01-05 16:00:17

SkillBase 不是 Subclass 的实例,那么您为什么认为强制转换会起作用呢?尝试一下:

SkillBase s = new Subclass();
Subclass sub = (Subclass)s;

这会成功。另外,我认为您对 instanceof 的理解不正确,我确信:

s instanceof Subclass

在上面的代码中产生 true 但在您的情况下产生 false

从现实世界的角度思考:您始终可以将 Dog 转换为 Animal,因为每只狗都是动物*,但将 Animal 转换为 Animal code> 到 Dog 可能会失败,因为有些动物不是狗。

* 事实上,编译器会为你做到这一点,这就是所谓的多态性

SkillBase is not an instance of Subclass, so why do you think casting will work? Try with this:

SkillBase s = new Subclass();
Subclass sub = (Subclass)s;

which will succeed. Also I think you are not correct with instanceof, I am certain that:

s instanceof Subclass

yields true in the the code above but false in your case.

Thinking in real world terms: you can always cast Dog to Animal because every dog is an animal*, but casting Animal to Dog might fail since some animals aren't dogs.

* in fact, compiler does that for you, it is known as polymorphism

久伴你 2025-01-05 16:00:17

您只能向上转换,即将子类分配给超类引用。可以这样想:Subclass 通过添加新方法 f() 来扩展 Superclass。现在超类 不知道关于 f() 的任何事情,因此也就出现了问题。

You can only up-cast i.e. assign sub classes to super class references. Think of it this way: Subclass extends the Superclass by adding a new method f(). Now Superclass doesn't know any thing about f() and hence the problem.

勿忘初心 2025-01-05 16:00:17

您可能使用了 instanceof 错误。

运行时会崩溃,因为 SkillBase 不是 Subclass

反之亦然。

You're probably using instanceof wrong.

The runtime is right to crash, as SkillBase is not a Subclass.

The other way around is true.

故人爱我别走 2025-01-05 16:00:17

s 无法转换为 Subclass,因为它没有实例化为 Subclass。这是一个技能库。如果你想使用子类为什么不直接实例化它呢?

Subclass s = new Subclass();

您仍然可以设置您想要的参数,因为它们已由 Skillbase 继承,并且不再需要强制转换。

s can't be cast to Subclass because it wasn't instantiated as a Subclass. It is a Skillbase. If you want to use a Subclass why not just instantiate it?

Subclass s = new Subclass();

You can still set the parameters you want because they hare inherited by Skillbase and the cast is no longer necessary.

梦在深巷 2025-01-05 16:00:17

问题是您没有正确使用铸造。一个类总是可以被强制转换为它的父类,但反之则不然。这个概念是子类(根据定义)知道父类的结构,并且(根据定义)已经支持父类中的所有签名。因此,父母是孩子的子集。

然而,反之则不然。父类对子类一无所知,也不知道子类是否在其结构中添加了额外的签名。因此,无法告诉编译器将父级视为子级并提供子级在父级中拥有的所有方法。

通俗地说,水(子)是液体(父),但并非所有液体都是水。因此,您想要对液体进行的任何测量(即:数量、粘度等)也适用于水,但反之则不然(例如:水的密度与油的密度完全不同)。

因此,为了将这一切带回您的情况,您可以将 Subclass 转换为 Skillbase,但反之则不然。

Subclass instanceof Skillbase == true
(Skillbase) new Subclass() - also valid
(SKillbase) new Sub#2 - also valid

ETC...

The problem is that you are not using casting properly. A class can always be cast as its parent, but not the other way around. The concept is that a child class (by definition) knows the structure of the parent, and (by definition) already supports all the signatures in the parent. Consequently, the parent is a subset of the child.

The inverse, however, is not true. The parent class knows nothing about the child class and/or whether the child has added extra signatures to its structure. Consequently, there is no way to tell the compiler to treat the parent as a child and make available all the methods that the child has in the parent.

It layman speak, water (child) is a liquid (parent), but not all liquids are water. Consequently, any measurements you want to make on liquids (ie: quantity, viscosity, etc) hold true for water as well, but not the other way around (ex: density of water is completely different than density of oil).

So to bring this all back to your situation, you can cast Subclass as a Skillbase but not the other way around.

Subclass instanceof Skillbase == true
(Skillbase) new Subclass() - also valid
(SKillbase) new Sub#2 - also valid

etc...

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