Nested Classes
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.
【增强 封装 可读 可维护】
Nested Classes的更多相关文章
- Java - Nested Classes
(本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) Nested Classes class OuterClas ...
- Java Nested Classes(内部类~第一篇英文技术文档翻译)
鄙人最近尝试着翻译了自己的第一篇英文技术文档.Java Nested Classes Reference From Oracle Documentation 目录 嵌套类-Nested Classes ...
- Java中的Nested Classes和Inner Classes
Java中的Nested Classes和Inner Classes Java有嵌套类(Nested Classes)和内部类(Inner Classes)的概念. 嵌套类(Nested Classe ...
- Nested Classes in C++
A nested class is a class which is declared in another enclosing class. A nested class is a member a ...
- Effective Java 13 Minimize the accessibility of classes and members
Information hiding is important for many reasons, most of which stem from the fact that it decouples ...
- Effetive Java 22 Favor static member classes over nonstatic
Nested class types Usage and remark Advantage Disadvantage static member classes Use for public help ...
- Visual C++ 里的 Classes, Methods and RTTI
类的基本布局 为了说明以下内容,让我们考虑这个简单的例子: class A { int a1; public: virtual int A_virt1(); virtual int A_virt2() ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十)之Inner Classes
The inner class is a valuable feature because it allows you to group classes that logically belong t ...
- .NET平台开源项目速览(18)C#平台JSON实体类生成器JSON C# Class Generator
去年,我在一篇文章用原始方法解析复杂字符串,json一定要用JsonMapper么?中介绍了简单的JSON解析的问题,那种方法在当时的环境是非常方便的,因为不需要生成实体类,结构很容易解析.但随着业务 ...
随机推荐
- java.lang.StackOverflowError at org.eclipse.jetty.util.resource.Resource.<init>(Resource.java:40)
今天做项目的时候,不知道哪根筋搭错了,多写了一句话,导致我忙活了一下午,各种百度,最后在朋友的帮助下,给了我思路,完美解决,不多说,上图. 我的登录页面引入了bootstrap.jsp的东西 解决问题 ...
- Tallest Cow
题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a p ...
- POJ 2411 Mondriaan's Dream [经典状态压缩dp]
题意:略. 思路:这一题开始做的时候完全没有思路,便去看了别人的题解. 首先,对于这个题目解法想有一个初步的了解,请看这里:http://www.2cto.com/kf/201208/146894.h ...
- XCode删除多余的Simulator(模拟器)
每个xocde都会自带一个模拟器,且都是随安装包一起打包的,比如xcode8.0的就自带iOS10的模拟器,这个是没办法删除. 但是对于想要用iOS10以前的模拟器,可以通过这里进行下载: 最终下载的 ...
- Linux基础学习3
鸟哥私房菜第十一章 read 用来读取键盘输入的变量,常被用在shell script的交互当中 [root@www ~]# read [-pt] variable 选项不参数: -p :后面可以 ...
- IOS7开发~NSAttributedString
从 NSBundle 中读取rtf文本文件的内容,然后用UITextView展示: NSURL *url = [[NSBundle mainBundle] URLForResource:@" ...
- ios内存管理笔记(一)
- Enter Query Mode Search Tricks Using Enter_Query Built-in in Oracle Forms
In this post you will learn how to specify any condition in enter query mode of Oracle Forms. Whenev ...
- 【Android】attr、style和theme
一.Attr 属性,风格样式的最小单元: Attr 的定义 在自定义 View 的时候,在 res/attrs.xml 文件中声明属性,而Android 系统的属性也是以同样的方式定义的.比如 lay ...
- 使用ssh从外网访问内网
一.场景如下: 各个角色的对应关系如下: 角色 描述 APP 个人笔记本,属于内网IP sshd server 公网 VPS ( 映射端口: port 2222 ),拥有公网IP ssh client ...