Nested Class
来源:互联网 发布:四旋翼控制算法stm32 编辑:程序博客网 时间:2024/06/04 19:58
1. Why Use Nested Classes?
There are several compelling reasons for using nested classes, among them:
- It is a way of logically grouping classes that are only used in one place.
- It increases encapsulation.
- Nested classes can lead to more readable and maintainable code.
2. Taxonomy of Classes
2.1 Static Nested Classes
A static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.
2.1.1 Create an object for the static nested class
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
2.1.2 When to use static nested class
B is A's assistant class and only be used by A, define B as A's static nested class like JDK's LinkedList and it's Entry.
2.2 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. Also, because an inner class is associated with an instance, it cannot define any static members itself.
2.2.1 Instantiate an inner class
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
2.2.2 Obtain outer class's reference
OuterClass.this
2.2.3 When to use
Inner class's most important feature is it can access outer class's any fields or methods, for example, in JDK's Collection
library, every collection must provide a according Iterator to support unified collection traversal.
2.3 Anonymous Inner Classes
An inner class within the body of a method without naming it
2.3.1 When to use
The class has few code to implement
- Nested Class
- Statc Nested Class用途
- 【Java】Nested Class
- inner class, static nested class
- local class and nested class
- Non-static Nested Class or Static Nested Class
- 嵌套类(Nested Class)
- Inner Class,Nested Class HashMap Collection Collections
- Static Nested Class 和 Inner Class区别
- java中的nested-class和inner-class
- [转]Nested Class 与 Inner Class
- Static Nested Class 和 Inner Class区别
- [书摘]使用Nested class进行代码测试
- final, static和nested class总结
- C++嵌套类nested class (内部类)
- 读书笔记:inner class和nested class的区别
- Java: 除了Inner Class之外的两种nested Class
- Static Nested Class 和 Inner Class的不同
- web.xml加入taglib报错
- VC 图像控件 图像显示
- repaint window caption
- 我配置的ant,build.xml文件用于导入到EJB
- 【动态规划】最大连续序列和最大子矩阵
- Nested Class
- 高维数组的理解
- 鲁迅名言——警醒自己
- .net 4.0 中对多线程新特性(一)
- 在VMware下通过安装VMware Tools配置Linux显卡
- 病理特征:Caused by: org.hibernate.HibernateException: cannot simultaneously fetch multiple bags,
- 被称为世上最经典的25句话
- 直接调用类成员函数地址
- 细微的用法