我有两个类A和B如下:基本运动在java中
public class A {
private int salary = 0;
protected void calculate() {
salary = 400;
}
protected A() {
calculate();
}
}
public class B extends A {
private int salary = 0; // (1)
protected void calculate() {
System.out.println("calculating salary...");
salary = 700;
}
public static void main(String[] args) {
System.out.println(new B().salary); // (2)
}
}
考虑线(2):我不明白为什么new B().salary
是有效的,因为工资的一致符(1) private
。你能帮我解释一下吗?
是您在B类中的主要方法还是仅仅是您已经布置代码段的方式? – DoctorMick
这是功课吗? –
@DoctorMick我的主要方法是在B类 –