小编art*_*emy的帖子

扩展超类和ClassCastException

我有一个超类,我想要覆盖两个方法.这是我的代码:

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  */
}
Run Code Online (Sandbox Code Playgroud)

我有一些构造函数,它将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 …
Run Code Online (Sandbox Code Playgroud)

java overriding casting classcastexception superclass

4
推荐指数
1
解决办法
1万
查看次数