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 ,这么做在于让程序耦合度更低,增加程序的健壮性 ...
随机推荐
- javascript 容错
window.onerror=function(){ return true;} 系统有onerror报错的代码,只要重新一下就可以在javascript错误的时候返回真(true)
- css之四大类选择器
一.选择器: 关于组合选择器之间的区别: 后代选择器(A B)与子选择器(A>B)之间的区别: 后代选择器: 子选择器: 总结:后代选择器的作用范围是一个元素里面包含的所有元素,即包括的是所有的 ...
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744943 记得我之前参与开发过一个华为的项目,要求程序可以支持好几种终端设备,其 ...
- Django的列表反序
Django虽然是python的web框架,但它不是所有的python特性都支持的. 最近在项目中遇到一个问题,需要在Django中将获得的列表反序排列,一开始我使用的是python的reverse方 ...
- Python 迭代删除重复项,集合删除重复项
1. 迭代删除重复项:先排序列表项,然后通过新迭代(not in)去除重复项,分片打印 def sanitize(time_string): if '-' in time_string: splitt ...
- android fragment getActivity()为空的另一个可能
目前这个方法得到空指针一般来说是因为Activity被销毁导致无法获取,但是开发中又出了一个低级错误导致getActivity为空. 因为我在Fragment的构造函数中调用这个方法了..此时Acti ...
- FB Flash Builder 安装错误 ERROR: DW050: - Microsoft Visual C++ 2010 Redistributable Package (x86): Install failed
这个问题很可能是你的 Microsoft Visual C++ 2010 Redistributable Package (x86) 太新的缘故,所以无法安装成功,导致最终的失败. 在控制面板-程序和 ...
- PLSQL_性能优化系列19_Oracle Explain Plan解析计划通过Profile绑定
20150529 Created By BaoXinjian
- GL_Oracle Erp月结和年节流程讨论(概念)
2014-02-06 Created By BaoXinjian
- GridView--scroolview嵌套listview和gridview
我们在真实项目中通常会遇到ListView或者GridView嵌套在ScrollView中问题.但是做的时候会发现,一旦两者进行嵌套,即会发生冲突.得不到我们希望的效果.由于ListView和Grid ...