我想知道为什么要调用
在类M中,in指的是类B中的以下函数:
。
z.f(-6);
在类M中,in指的是类B中的以下函数:
public void f(double y) {
this.x = (int) y + B.y;
}
不要在A类中使用函数f,因为b.x已经被A类覆盖。相反,应该使用
public void f (int y) {
this.x = y*2;
B.y = this.x;
}
在B类中,至少参数类型匹配。
完整代码如下:
public class A {
public int x = 1;
public A(int x) {
this.x += x;
}
public A (double x) {
x += x;
}
public void f(double x) {
this.x = this.x + (int) (x + B.y);
}
}
public class B extends A {
public static int y = 3;
public int x = 0;
public B (double x) {
super((int) x);
}
public void f(int y) {
this.x = y*2;
B.y = this.x;
}
public void f(double y) {
this.x = (int) y + B.y;
}
}
public class M {
public static void main (String[] args){
A a = new A(B.y);
a.f(1);
B b = new B(3.0);
A z = b;
z.f(-5.0);
z.f(-6);
System.out.println(b.x + " " + z.x);
}
}