Java 中的类转换异常
我在相互转换类时遇到问题。
要更详细地解释它,请看一下这张图片。
在我的代码中,我执行 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SkillBase
不是Subclass
的实例,那么您为什么认为强制转换会起作用呢?尝试一下:这会成功。另外,我认为您对
instanceof
的理解不正确,我确信:在上面的代码中产生
true
但在您的情况下产生false
。从现实世界的角度思考:您始终可以将
Dog
转换为Animal
,因为每只狗都是动物*,但将Animal
转换为Animal
code> 到Dog
可能会失败,因为有些动物不是狗。* 事实上,编译器会为你做到这一点,这就是所谓的多态性
SkillBase
is not an instance ofSubclass
, so why do you think casting will work? Try with this:which will succeed. Also I think you are not correct with
instanceof
, I am certain that:yields
true
in the the code above butfalse
in your case.Thinking in real world terms: you can always cast
Dog
toAnimal
because every dog is an animal*, but castingAnimal
toDog
might fail since some animals aren't dogs.* in fact, compiler does that for you, it is known as polymorphism
您只能向上转换,即将子类分配给超类引用。可以这样想:
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 theSuperclass
by adding a new methodf()
. NowSuperclass
doesn't know any thing about f() and hence the problem.您可能使用了
instanceof
错误。运行时会崩溃,因为
SkillBase
不是Subclass
。反之亦然。
You're probably using
instanceof
wrong.The runtime is right to crash, as
SkillBase
is not aSubclass
.The other way around is true.
s 无法转换为 Subclass,因为它没有实例化为 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?
You can still set the parameters you want because they hare inherited by Skillbase and the cast is no longer necessary.
问题是您没有正确使用铸造。一个类总是可以被强制转换为它的父类,但反之则不然。这个概念是子类(根据定义)知道父类的结构,并且(根据定义)已经支持父类中的所有签名。因此,父母是孩子的子集。
然而,反之则不然。父类对子类一无所知,也不知道子类是否在其结构中添加了额外的签名。因此,无法告诉编译器将父级视为子级并提供子级在父级中拥有的所有方法。
通俗地说,水(子)是液体(父),但并非所有液体都是水。因此,您想要对液体进行的任何测量(即:数量、粘度等)也适用于水,但反之则不然(例如:水的密度与油的密度完全不同)。
因此,为了将这一切带回您的情况,您可以将
Subclass
转换为Skillbase
,但反之则不然。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 aSkillbase
but not the other way around.etc...