public class A {
public int m = 1;
public void view() {
System.out.println(this.m);
}
}
class B extends A {
public int m = 2;
public static void main(String[] args) {
B b = new B();
System.out.println(b);
b.view();
}
}
输出
test.B@6ca6fed5
1
public int m = 1;
public void view() {
System.out.println(this.m);
}
}
class B extends A {
public int m = 2;
public static void main(String[] args) {
B b = new B();
System.out.println(b);
b.view();
}
}
输出
test.B@6ca6fed5
1
public int m = 2;
public static void main(String[] args) {
Demo3 b = new Demo3();
A a =new A();
System.out.println(b.m);//2
System.out.println(a.m);//1
b.view();//1
}
}class A {
public int m = 1;
public void view() {
System.out.println(m);
}
}
要调用当前类对象中的m,覆写view就可以了。建议使用调试工具
属性确实是没有多态,两个是指向同样的,this不是应该指向子类的m