日期:2014-05-20  浏览次数:20761 次

抱歉,我也不想炒老帖。java private属性继承问题
Java code

public class Test extends A
{
    public static void main(String[] args)
    {
        System.out.println(new Test().a);
        System.out.println(new Test().b);
    }
}

class A
{
    private int a;
}

报错如下:
The field A.a is not visible
b cannot be resolved or is not a field
这是不是可以说明,private 的a被继承了呢?

------解决方案--------------------
引用Java Language Specification 3的原话:
A private class member or constructor is accessible only within the body of the
top level class (§7.6) that encloses the declaration of the member or constructor. It
is not inherited by subclasses.