【发布时间】:2020-03-10 15:16:22
【问题描述】:
在我的代码中,我有 3 个类:
班级A
public class A {
int x = 1;
static double y = 3.0;
public A() {
x = (int)y;
}
public A(int x) {
this .x = this . getX () + x;
}
int getX () {
return this .x;
}
public void f( float z) {
y *= z;
}
}
类B,扩展A
public class B extends A {
int x = 7;
public B(int x) {
super (x);
this .x = x;
}
public B() {
this .y += 1.0;
}
int getX () {
return this .x;
}
public void f( long z) {
y += z;
}
public void f( double z) {
y -= z;
}
}
和类M,其中包括main函数
public class M {
public static void main ( String [] args ) {
A a1 = new A();
System .out. println (a1.x + " " + A.y); // OUT: [ ] [ ]
B b1 = new B();
System .out. println (b1.x + " " + A.y); // OUT: [ ] [ ]
System .out. println ((( A)b1 ).x); // OUT: [ ]
A ab = new B (5);
System .out. println (ab.x + " " + A.y); // OUT: [ ] [ ]
System .out. println ((( A)ab ).x); // OUT: [ ]
A.y = 5.0;
b1.f (2.0f);
System .out. println (A.y); // OUT: [ ]
ab.f (5);
System .out. println (A.y); // OUT: [ ]
}
}
main 方法中的每个调用函数都按预期运行,但以下两个除外:
A ab = new B (5);
System .out. println (ab.x + " " + A.y); // OUT: [ ] [ ]
System .out. println ((( A)ab ).x); // OUT: [ ]
这给了
5 4.0
5
作为输出,而我希望它给出
6 4.0
6
现在,据我了解,在创建 B 类型的新对象时,该对象由 A 类型的引用变量 ab 引用,在调用正确的构造函数后,变量 x超类中的值应为6,而子类中的另一个x 等于5。
如果这是对的,那么,ab.x 不应该引用超类中的x,它等于6,因为处理了属性和静态方法(与非静态方法相反)在编译时,这意味着println 方法应该在与引用变量类型相同的类中打印变量?
如果是(( A)ab ).x),在B 类中的x 不应该在投射ab 后不可见吗?
如果您能指导我了解为什么会发生这种情况,我将不胜感激。
【问题讨论】:
-
A构造函数中的this .x = this . getX () + x;使用了B中的重写函数getX,这就是原因
标签: java oop polymorphism