读书人

抱歉小弟我也不想炒老帖。java priv

发布时间: 2012-01-10 21:26:51 作者: rapoo

抱歉,我也不想炒老帖。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.

读书人网 >J2SE开发

热点推荐