如果类B被定义在类A的内部,类B称为类A的内部类,类A称为类B的包围类。类B仅为类A所知,仅限类A所用。内部类B可以访问包围类A的所有成员,包括私有成员;但反之则不行,因为内部类的成员仅限其内部使用,不能被其外部的任何类使用。 我觉得这句话,好象反了.... class A { private ....; class B{...} }
//新曙光培训@西安 //www.xinshuguang.org public class A{ private static int i; class B{ private int k; B(){i=8;}//内部keyi } public void f(){ B bb=new B(); } public static void main(String[] args){ A aa=new A(); aa.f(); System.out.println(i); // aa.k=9; 外部不能访问内部 } }
你直接从内部类访B访问A的数据看看,再用A访问B的数据看看,编译一下不就知道了吗
{
private int i;
class B{...}
} b里面可以这样用 A.this.i = ....;
不可能吧...
整个class都在A的肚子里,也不行??
//www.xinshuguang.org
public class A{
private static int i;
class B{
private int k;
B(){i=8;}//内部keyi
}
public void f(){
B bb=new B();
}
public static void main(String[] args){
A aa=new A();
aa.f();
System.out.println(i);
// aa.k=9; 外部不能访问内部
}
}