http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

package priceton;

/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ public class ShadowTest { public int x = 0; class FirstLevel { public int x = 1; void methodInFirstLevel(int x) {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
}
} public static void main(String... args) {
ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
fl.methodInFirstLevel(23);
}
}

  

Why Use Nested Classes?

Compelling reasons for using nested classes include the following:

  • It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

  • It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

  • It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.

【增强 封装 可读 可维护】

最新文章

  1. Teamcenter 2007 "由于某些意外的 DBMS 错误失败"
  2. 在 Excel 工作簿中定义决策表(Oracle Policy Modeling-Define decision tables in Excel workbooks)
  3. MYSQL C API : mysql_real_escape_string 二进制数据存储
  4. hadoop pipes wordcount compile
  5. cocos2d CCArray
  6. How to using to code import to GL journal[AX2012]
  7. QTableView带可编辑进度条
  8. Java log4j使用
  9. 插入排序的性能测试对比(C与C++实现)
  10. JavaScript(第十五天)【匿名函数和闭包】
  11. Java-Maven(三):Maven坐标、Maven仓库、Maven生命周期
  12. LAMP构架搭建论坛
  13. 怎么将XML字符串转换为XmlDocument,并获取部分节点值
  14. 刘志梅201771010115.《面向对象程序设计(java)》第六周学习总结
  15. Java语法之反射
  16. 为小米(红米)6A解锁_ROOT_安装天下游虚拟定位教程_已亲身验证通过!附图
  17. SPRINT四则运算(第二天)
  18. notepad++颜色修改
  19. 20172306《Java程序设计与数据结构》第十周学习总结
  20. 谈谈HashMap线程不安全的体现

热门文章

  1. luogu P1345 [USACO5.4]奶牛的电信Telecowmunication
  2. (入门SpringBoot)SpringBoot发送邮件(十一)
  3. [ios]objective-c 协议和委托 (重点基础知识)
  4. ios开发小结之app发布升级
  5. 安装xz
  6. Context都没弄明白,还怎么做Android开发?
  7. 状态压缩DP常遇到的位运算
  8. [Android实例] Scroll原理-附ScrollView源码分析 (转载)
  9. Checkbox Text 重影问题的解决的方法
  10. java 几个线程池的理解