java 扩展超类和 ClassCastException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4260864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
extending superclass and ClassCastException
提问by artemy
I have a superclass, which two methods i want to override. Here's my code:
我有一个超类,我想覆盖这两个方法。这是我的代码:
public class MyCustomClass extends SomeSuperClass {
protected MyCustomClass(params) {
super(params);
}
@Override
public void method1() {
super.method1();
/* here goes my code */
}
@Override
public void method2() {
super.method2();
/* here goes my another code */
}
I have some constructor, that passes SomeSuperClass object as a parameter, and what i do next:
我有一些构造函数,它将 SomeSuperClass 对象作为参数传递,接下来我要做什么:
MyCustomClass object;
/* now i have object of type SomeSuperClass,
but with my own method1() and method2() */
object = (MyCustomClass) MyCustomClass.item(blahblah);
/* eclipse suggests casting, because MyCustomClass.item()
constructor still returns SomeSuperClass object */
otherobject = OtherConstructor.object(object);
//OtherConstructor passes SomeSuperClass object
That seems to be right, but i'm getting java.lang.ClassCastException in SomeSuperClass while executing.
这似乎是正确的,但我在执行时在 SomeSuperClass 中得到 java.lang.ClassCastException 。
if i create SomeSuperClassObject, i lose my overriden methods.
如果我创建 SomeSuperClassObject,我将丢失我的重写方法。
With casting, even if there's no errors in eclipse, application crashes. In other words, how i can override SomeSuperClass with my own methods, and still get SomeSuperClass object to use with OtherConstructor? If it is important, this code is for android app.
使用强制转换,即使 eclipse 中没有错误,应用程序也会崩溃。换句话说,我如何用我自己的方法覆盖 SomeSuperClass,并且仍然让 SomeSuperClass 对象与 OtherConstructor 一起使用?如果这很重要,此代码适用于 android 应用程序。
回答by Powerlord
As a general rule, you can cast an instance of a subclass to its parent class:
作为一般规则,您可以将子类的实例转换为其父类:
MyCustomClass object = new MyCustomClass(params);
SomeSuperClass superClass = (SomeSuperClass) object;
However, you cannot cast an instance of a superclass to a subclass:
但是,您不能将超类的实例转换为子类:
SomeSuperClass object = new SomeSuperClass(params);
MyCustomClass customClass = (MyCustomClass) object; // throws ClassCastException
This is because a MyCustomClass
object is also a SomeSuperClass
object, but not all SomeSuperClass
objects are MyCustomClass
objects.
这是因为MyCustomClass
对象也是SomeSuperClass
对象,但并非所有SomeSuperClass
对象都是MyCustomClass
对象。
You may be able to work around this with certain design patterns. Java itself tends to use the Decorator patterna lot.
您可以使用某些设计模式解决此问题。Java 本身倾向于大量使用装饰器模式。
回答by Dave Costa
If the item()
method is declared in SomeSuperClass
, I doubt that it is returning an instance of MyCustomClass
. So your cast (MyCustomClass) MyCustomClass.item(blahblah)
would be invalid.
如果该item()
方法是在 中声明的SomeSuperClass
,我怀疑它是否正在返回 的实例MyCustomClass
。所以你的演员表(MyCustomClass) MyCustomClass.item(blahblah)
将是无效的。
回答by malejpavouk
From what I see it seems that MyCustomClass.item(blahblah) call returns something different (maybe the parent) than MyCustomClass. Its the only part in te code, where you are casting object...
从我看来,MyCustomClass.item(blahblah) 调用返回的东西(可能是父代)与 MyCustomClass 不同。它是 te 代码中唯一的部分,您在其中投射对象...
回答by artemy
looks like problem solved. I tried
看起来问题解决了。我试过
object = new MyCustomClass(blahblah);
and it worked. BTW, can somebody explain that?
它奏效了。顺便说一句,有人可以解释一下吗?