浏览 1149 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-12-02
下面的代码证明了这一点。个人理解是:内部类对于 包含它的类 来说毫无隐私——此处包含至少嵌套了一级。 class FirstLevel{ private class SecondLevel{ private int i=10; private void say(){ System.out.println("private SecondLevel.say()"); } private class ThirdLevel{ private int i=12; private void say(){ System.out.println("private ThirdLevel.say()"); } } } void haha(){ System.out.println("FirstLevel.haha() BEGIN:"); SecondLevel secondLevel=new SecondLevel(); SecondLevel.ThirdLevel thirdLevel=secondLevel.new ThirdLevel(); System.out.println("SecondLevel's private i"+secondLevel.i); secondLevel.say(); System.out.println("ThirdLevel's private i :"+thirdLevel.i); secondLevel.say(); System.out.println("FirstLevel.haha() END.\n"); } public static void main(String[] arguments){ FirstLevel firstLevel=new FirstLevel(); firstLevel.haha(); FirstLevel.SecondLevel secondLevel=firstLevel.new SecondLevel(); secondLevel.say(); FirstLevel.SecondLevel.ThirdLevel thirdLevel=secondLevel.new ThirdLevel(); thirdLevel.say(); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |