当前位置: 代码迷 >> J2SE >> 抱歉,小弟我也不想炒老帖。java private属性继承有关问题
  详细解决方案

抱歉,小弟我也不想炒老帖。java private属性继承有关问题

热度:219   发布时间:2016-04-24 12:34:33.0
抱歉,我也不想炒老帖。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.
  相关解决方案