简体   繁体   English

Java中的类转换异常

[英]Class Casting Exception in Java

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(); 在我的代码中,我执行SkillBase s = new SkillBase(); and then set some values in that class. 然后在该类中设置一些值。 Then I try to do Subclass sub = (Subclass)s; 然后我尝试做Subclass sub = (Subclass)s; but when running it it gives a ClassCastException. 但是运行时会给出ClassCastException。

I added a small debug part, which checks if it's instanceof which returns true. 我添加了一个小的调试部分,该部分检查它的instanceof是否返回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) 我已经尝试过Google,在这里也看到了一些问题并阅读了它们(没有一个答案适合我)

So, what do I do? 那么,我该怎么办?

SkillBase is not an instance of Subclass , so why do you think casting will work? SkillBase不是Subclass的实例,那么您为什么认为强制转换会起作用? 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: 另外我认为您对instanceof并不正确,我可以确定:

s instanceof Subclass

yields true in the the code above but false in your case. 在上面的代码中产生true ,但在您的情况下产生false

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. 用现实世界来思考:因为每只狗都是动物* ,所以您总是可以将DogAnimal * ,但是由于某些动物不是狗,因此将AnimalDog可能会失败。

* in fact, compiler does that for you, it is known as polymorphism *实际上,编译器会为您执行此操作,这被称为多态

You can only up-cast ie assign sub classes to super class references. 您只能向上广播,即将子类分配给超类引用。 Think of it this way: Subclass extends the Superclass by adding a new method f() . 这样考虑: Subclass Superclass通过添加新方法f()扩展了Superclass Now Superclass doesn't know any thing about f() and hence the problem. 现在, Superclass对f()一无所知,因此也不知道问题所在。

You're probably using instanceof wrong. 您可能使用了instanceof错误。

The runtime is right to crash, as SkillBase is not a Subclass . 由于SkillBase不是Subclass ,因此运行时很容易崩溃。

The other way around is true. 反之亦然。

s can't be cast to Subclass because it wasn't instantiated as a Subclass. s不能转换为Subclass,因为它没有实例化为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. 您仍然可以设置所需的参数,因为它们已由Skillbase继承,并且不再需要强制转换。

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. 因此,为了使这一切回到您的情况,您可以将SubclassSkillbase不行。

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

etc... 等等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM