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 ,这么做在于让程序耦合度更低,增加程序的健壮性 ...
随机推荐
- 别样的checkbox
<style type="text/css"> input[type=checkbox] { visibility: hidden; } .slide_check_bo ...
- 开始学习IOS
最好的学习方式就是动手. 对于有编程经验的程序员来说,学习另外一门技术最好的方式就是coding,虽然基础知识和IDE都不熟悉,但是在写代码的过程中,不断的解决问题,不断查找资料,不断感悟代码,一切都 ...
- 04文件与IO
文件系统调用: open.close.create.read.write open: int open(const char* path, int flags) path:要打开的文件的路径 flag ...
- golang的验证码相关的库
识别库 https://github.com/goghcrow/capture_easy 生成验证码的库 https://github.com/hanguofeng/gocaptcha 生成图片水印 ...
- ubuntu eclipse下配置C++ 环境
首先你通过以下3个命令确定已安装好eclipse cdt sudo apt-get install eclipse sudo apt-get install eclipse-pde sudo apt- ...
- win7下vs2008编译出现C1859错误的处理办法
昨天,电脑上安装的vs2008 sp1版本编译工程时候出了这样的错了:fatal error C1859: 'Debug\**.pch'.清理并重新编译会临时解决问题,但问题仍然会频发.后面上网找资料 ...
- mapreduce计算框架
一. MapReduce执行过程 分片: (1)对输入文件进行逻辑分片,划分split(split大小等于hdfs的block大小) (2)每个split分片文件会发往不同的Mapper节点进行分散处 ...
- 关于Linux中exec的一点心得
最近在学习linux操作系统中的相关知识,在使用execlp系统调用时,发现了些有趣的东西. 首先,关于execlp函数的用法: int execlp(const char *file, const ...
- (MVVM) button enable 时,UI没有被刷新。
if (!this.CanExecuteSubmitButton) { this.CanExecuteSubmitButton = true; CommandManager.InvalidateReq ...
- 对象生命周期及crud操作
1. 对象状态及生命周期 瞬时状态: 持久状态: 游离状态: 2. new->save->close->update public void testSave(){ Session ...