假设我有以下Java类:
Class A:
为什么编译器会在这一行标记对x的访问?
Class A:
public class A {
private int x;
public A(int x){
this.x = x;
}
public static void main(String[] args) {
A a = new A(1);
B b = new B(1,2);
System.out.println((A)b.x);
}
}
Class B:
public class B extends A {
public int y;
public B(int x, int y){
super(x);
this.y = y;
}
}
为什么编译器会在这一行标记对x的访问?
System.out.println((A)b.x);
为什么我在定义x的类中使用x时会出错?
可能是由以下原因引起的:
1. 多态性的使用?
2. 静态方法的使用?
3. main方法的使用?