Java - Nested Classes
(本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html)
Nested Classes
class OuterClass {
......
class StaticNestedClass {
......
}
......
class InnerClass {
......
}
定义
nested classes (嵌套类)分为两种:non-static与static,前者通常称作inner classes,后者称作static nested classes。
作用域与访问范围
两种nested classes都是其enclosing class的内部成员,non-static classes可以引用到包装类的private成员,static classes没有这个能力。两种nested classes都可以被声明为public/protected/private/package private。
作用场景(因原文概括性高,直接引用)
- It is a way of logically grouping classes that only used in one place.
- It increases encapsulation.
- It can lead to more readable and maintainable code.
Static Nested Classes
- static nested classes不可直接调用其外部类的方法/成员,必须通过一个外部类的实例才能访问(在这一点上,static inner class表现的如同一个top-level class)
- 用如下方式创建一个static nested class实例
OuterClass.StaticNestedClass foo = new OuterClass.StaticNestedClass();
Inner Classes
- As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields.
- Cause an inner class is associated with an instance, it cannot define any static members itself.
- An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.
- 用如下方式创建一个inner class实例
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
- 有两种特殊的inner classes:local classes和anonymous class
Shadowing(不知确切的中文翻译是什么,“遮蔽”?)
原文用一段很简洁的代码讲清了这个问题
public class Main {
public static void main(String[] args) throws Exception {
(new OuterClass()).new InnerClass().printX(2);
System.out.println("FINISH!");
}
}
class OuterClass {
int x = 0;
class InnerClass {
int x = 1;
public void printX (int x) {
System.out.println("x=" + x);
System.out.println("this.x=" + this.x);
System.out.println("OuterClass.this.x=" + OuterClass.this.x);
}
}
}
输出:
x=2
this.x=1
OuterClass.this.x=0
FINISH!
Serialization
- Serialization of inner classes, including local and annonymous classes, is strongly discouraged.
- 原因是编译器在compile certain constructs时,会产生synthetic constructs:these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code.
- You may have compatibility issues if you serialize an inner class and then deserialize it with a different JRE implementation.
Java - Nested Classes的更多相关文章
- 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
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html package priceton; /* * Copyright (c) ...
- Top 15 Java Utility Classes
In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...
- java 错误 classes路径配置错误
1. 错误显示页 2. 解决步骤 2.1. 查看 root cause 信息 org.springframework.beans.factory.BeanCreationException: Erro ...
- Java Inner Classes
When thinking about inner classes in java, the first thing that comes to my mind is that, WHY do we ...
- Summary of java stream classes
Java’s stream classes are good for streaming sequences of bytes, but they’re not good for streaming ...
- Java Date Classes
References: [1] http://tutorials.jenkov.com/java-date-time/index.html [2] https://docs.oracle.com/ja ...
- Effective Java Chapter4 Classes and Interface
MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...
随机推荐
- web前端基础知识及快速入门指南
web前端基础知识及快速入门指南 做前端开发有几个月了,虽然说是几个月,但是中间断断续续的上课.考试以及其它杂七杂八的事情,到现在居然一直感觉自己虽然很多前端的知识很眼熟,却也感觉自己貌似也知识在门口 ...
- 【转】深入分析 iBATIS 框架之系统架构与映射原理
深入分析 iBATIS 框架之系统架构与映射原理 iBATIS 通过 SQL Map 将 Java 对象映射成 SQL 语句和将结果集再转化成 Java 对象,与其他 ORM 框架相比,既解决了 Ja ...
- H5元素
下面整理下我用到的H5元素 placeholder 显示input输出口的默认提示文本 <input type="search" name="user_search ...
- TX Textcontrol 使用总结三——禁用右键、模版合并
一.Tx Textcontrol如何禁用右键快捷菜单? ==> 添加txContent_TextContextMenuOpening事件,实现方式如下所示: private void txCon ...
- web几个高性能框架的简单测试
参考的这里 压测工具 wrk -t16 -c100 -d30s http://127.0.0.1:8080/rest/hello 测试代码 package main import ( "st ...
- SQLServer数据库字典维护方法
启用SQLServer启用管理器,以2008为例 1.设置表信息描述 选中要设置的表,右键点击“属性” . 选择扩展属性 填写要求: 名称:MS_Description 值: 模块名称-表名称 修改语 ...
- php没有开启Memcache扩展类时
模拟PHP Memcache 类.当服务器没有开启Memcache扩展的时候.可以采用本类使用方法class_exists('Memcache') or include './Memcache.cla ...
- (C# Debug)A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll
Debug 模式下运行程序的时候,Output 窗口出来个错误“A first chance exception of type 'System.ArgumentException' occurred ...
- DBA_Oracle Startup / Shutdown启动和关闭过程详解(概念)
2014-08-07 Created By BaoXinjian
- HDU 2087 剪花布条(KMP,不可重叠重复子串)
给KMP传的数组一定要从0开始!! 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 这题和我上一篇博客类似,只不过不可重叠,我看了数据范围不大,所以就开 ...