我了解我的AP计算机科学类的继承,被这个问题困惑:为什么此代码发生错误?
查找下面的代码产生的输出:
public class A {
private int xx;
public A() {
xx = 1;
}
public A(int x) {
xx = x;
}
public void addX(int x) {
xx += x;
}
public void multX(int x) {
xx *= x;
}
public int getXX() {
return xx;
}
public String toString() {
return ""+xx;
}
}
public class B extends A {
private int yy;
public B(int y) {
yy = y;
}
public void addY(int y){
yy += y;
}
public void multY(int y) {
yy *= y;
}
public int getYY() {
return yy;
}
public void addBtoA(){
addX(yy);
}
public String toString() {
return ""+yy;
}
}
客户端代码:
A a = new A(14);
B b = new B(33);
b.addBtoA();
System.out.println(a.getYY());
我不明白为什么这会导致错误。请帮忙!
什么“错误”?这与预期有何不同?和*为什么*?请明确点。 – user2864740