我想对超类 Object 调用子类方法,但是由于我在子类中声明了另一个整数,所以它给了我一个异常,是否有解决方法来实现这一点?
public static void main(String[]args){
A a = new A(2,3);
B b = new B(3,5,6);
System.out.println("A object: ");
a.showij();
System.out.print("sum = ");
((B) a).sum(); < ==== this line gives me the error, can't cast
System.out.println("B object: ");
b.showij();
System.out.print("sum = ");
b.sum();
}
public class B extends A {
int k;
public B(){
super();
}
public B(int a, int b, int c) {
super(a, b);
k = c;
}
public void sum(){
System.out.println( i + j + k);
}
}
public class A {
int i,j;
public A() {
}
public A( int a, int b){
i = a;
j = b;
}
public void showij(){
System.out.println("\ti: " + i + " j: " + j);
}
}
*编辑:这是整个事情