public class A4 {
class B {
private int b1 = 1;
} public void test() {
B b = new B();
int v3 = b.b1;
System.out.println(v3);
} public static void main(String[] args) {
new A4().test();
}
}在外部类A4 test()里 int v3 = b.b1;  为什么能访问内部类B的私有成员?

解决方案 »

  1.   

    因为b是B的实例,它当然可以访问B的私有成员b1啦
      

  2.   

    b是类B的实例对象 B有一个私有成员b1
    如果是在外部就不能访问 
    这里就是内部类的与其他类的区别了
      

  3.   

    因为class B 是属于A4的一个成员(也就是类成员),既然是类成员,那么在类的内部,类成员对象就可以访问类成员的私有属性。
      

  4.   

    B的对象当然能访问B的private变量
      

  5.   

    呵呵,int v3 = b.b1; 自己的东西自己当然能够用啦;
    你应该用A4的实例去访问b1,然后再问:外部类A4为什么能访问内部类B的私有成员?
      

  6.   


    类成员对象就可以访问类成员的私有属性?是么?public class Student 
    {
    private String name = "lcf";
    }
    public class TestGouZao {
        Student stu = new Student();
        public static void main(String[] args) {
    System.out.println(stu.name);
    }
    }你看能访问到stu的name属性么?