- 浏览: 804683 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (360)
- Java (101)
- JPA/Hibernate (10)
- Spring (14)
- Flex/BlazeDS (37)
- Database (30)
- Lucene/Solr/Nutch (0)
- Maven/Ant (25)
- CXF/WebService (3)
- RPC/RMI/SOAP/WSDL (1)
- REST (6)
- TDD/BDD/JUnit (1)
- Servlet/JSP (2)
- AI/MachineLearning (3)
- Resource (1)
- 字符编码 (2)
- OOA/OOPS/UML (5)
- DesignPattern (8)
- 算法与数据结构 (11)
- Web&App Server (13)
- 并发&异步&无阻塞 (7)
- Entertainment (4)
- JavaScript/ExtJS (45)
- CodeStyle&Quality (1)
- svn/git/perforce (8)
- JSON (2)
- JavaScriptTesting (4)
- Others (6)
- RegularExpression (2)
- Linux/Windows (12)
- Protocal (2)
- Celebrities (1)
- Interview (1)
- 计算机语言 (1)
- English (2)
- Eclipse (5)
- TimeZone/时区 (1)
- Finance (1)
- 信息安全 (1)
- JMS/MQ (2)
- XSD/XML/DTD (3)
- Android (4)
- 投资 (3)
- Distribution (3)
- Excel (1)
最新评论
-
qdujunjie:
如果把m换成具体的数字,比如4或者5,会让读者更明白
m阶B树中“阶”的含义 -
java-admin:
不错,加油,多写点文章
关于Extjs的mixins和plugin -
xiehuaidong880827:
你好,我用sencha cmd打包完本地工程后,把app.js ...
ExtJS使用Sencha Cmd合并javascript文件为一个文件 -
KIWIFLY:
lwpan 写道inverse = "true&qu ...
Hibernate中什么时候使用inverse=true -
luedipiaofeng:
good
消除IE stop running this script弹出框
要点:
1) 外围类不能直接访问嵌套类的成员,不论嵌套类是静态的还是非静态的,原因很简单,如果只实例化了外围类的对象,根本没有创建出嵌套类的对象,那外围类的对象访问什么?
2) 非静态的嵌套类才称为“内部类”(inner class),静态的嵌套类就称为"静态嵌套类"
3) 内部类的实例化是通过外围类的实例new出来的,即必须现有外围类对象存在,
OuterClass.InnerClass innerObject = outerObject.new InnerClass(),通过这种方式我们也很容易看出为什么InnerClass 能直接访问OuterClass的成员
4)静态嵌套类可以理解成一个封装在外围类里面的顶层类,也就是说静态嵌套类的使用方式跟顶层类是没太大区别的:
OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();
那么当然静态嵌套类也就不能像内部类那样可以直接访问外围类的成员,原因也很简单,你可以单独实例化一个静态嵌套类的对象,而不实例化外围类对象,如果可以访问,那么静态嵌套类的对象访问的是什么?
5)一个文件中public顶层类只能有一个,但是如果有嵌套类,嵌套类可以是public,protected,default,private,另外顶层类也只能是public和default级别,你不能对顶层类使用protected和private,default也可称package private
Outer class do not have access to nested class members directly and have to do it through a instance of the nested class.
Nested Classes
The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:
class OuterClass {
...
class NestedClass {
...
}
}
Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes.
class OuterClass {
...
static class StaticNestedClass {
...
}
class InnerClass {
...
}
}
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)
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.
Static Nested Classes
As with class methods and variables, 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.
Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
Static nested classes are accessed using the enclosing class name:
OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:
OuterClass.StaticNestedClass nestedObject =
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. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:
class OuterClass {
...
class InnerClass {
...
}
}
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.
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
There are two special kinds of inner classes: local classes and anonymous classes.
Shadowing
If a declaration of a type (such as a member variable or a parameter name) in a particular scope (such as an inner class or a method definition) has the same name as another declaration in the enclosing scope, then the declaration shadows the declaration of the enclosing scope. You cannot refer to a shadowed declaration by its name alone. The following example, ShadowTest, demonstrates this:
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);
}
}
The following is the output of this example:
x = 23
this.x = 1
ShadowTest.this.x = 0
This example defines three variables named x: the member variable of the class ShadowTest, the member variable of the inner class FirstLevel, and the parameter in the method methodInFirstLevel. The variable x defined as a parameter of the method methodInFirstLevel shadows the variable of the inner class FirstLevel. Consequently, when you use the variable x in the method methodInFirstLevel, it refers to the method parameter. To refer to the member variable of the inner class FirstLevel, use the keyword this to represent the enclosing scope:
System.out.println("this.x = " + this.x);
Refer to member variables that enclose larger scopes by the class name to which they belong. For example, the following statement accesses the member variable of the class ShadowTest from the method methodInFirstLevel:
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
本文来自:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html, 这是官方文档,里面还有实际例子.
中文参考内容
Java内部类详解
https://www.cnblogs.com/dolphin0520/p/3811445.html
http://younglab.blog.51cto.com/416652/106059
英文参考内容
https://blogs.oracle.com/darcy/entry/nested_inner_member_and_top
http://javarevisited.blogspot.hk/2011/11/static-keyword-method-variable-java.html
发表评论
-
sapjco3 notes
2019-03-21 14:51 1164sapjco https://support.sap.com/ ... -
使用RestTemplate发送post JSON请求
2019-01-12 17:30 4742private final String BASE_URL = ... -
使用RestTemplate发送post JSON请求
2019-01-12 17:30 3510private final String BASE_URL = ... -
Spring线程池ThreadPoolTaskExecutor
2018-08-06 09:51 1889<!-- spring thread pool ex ... -
Spring注解事物@Transactional不工作
2018-08-02 18:50 2679“In proxy mode (which is the de ... -
创建前缀索引报长度超出错误
2018-07-25 15:44 1732表结构定义如下: CREATE TABLE `sku` ( ` ... -
Mysql Varchar字符长度
2018-07-25 15:23 1353`sku_name` VARCHAR(200) NOT NUL ... -
使用 Spring RestTemplate 发送 post 请求
2018-07-23 18:49 11728注意点: 1)使用MultiValueMap设置入参,不要使 ... -
Java动态代理Dynamic Proxy
2018-07-21 16:33 815JAVA学习篇--静态代理VS动态代理 https://blo ... -
分布式实时日志分析解决方案 ELK 部署架构
2018-07-20 09:52 1190原文链接:http://www.importn ... -
为什么HashMap容量一定要为2的幂呢?
2018-07-19 10:07 1728原文链接:https://blog.csdn.net/wang ... -
为什么计算HashCode时通常选择31这个数?
2018-07-19 10:05 1423摘自http://www.importnew.com/2208 ... -
jackson自定义序列化和反序列化
2018-07-10 18:47 2244原文链接:https://blog.csdn.net/liu ... -
Pay special attention when modifying online running system
2017-06-23 10:25 0Never remove any properties, me ... -
Map中的Null key, Null Value
2017-06-14 10:52 1946ConcurrentHashMap的key和value都不能为 ... -
Java语法糖
2017-06-05 20:03 531Java语法糖之foreach http://www.imp ... -
Java集合相关
2017-05-24 17:55 0Java集合框架:ArrayList http://www. ... -
Java数据类型的转换:隐式(自动)转换与强制转换
2017-05-14 10:46 0http://blog.csdn.net/u011240877 ... -
分布式开放消息系统(RocketMQ)的原理与实践
2017-05-07 19:55 731分布式开放消息系统(RocketMQ)的原理与实践 http ... -
面试知识点复习(Interview knowledge review)
2017-05-07 18:39 0JVM,多线程相关知识 http://darrenzhu.it ...
相关推荐
嵌套类主要分为两大类:静态嵌套类(Static Nested Class)和非静态嵌套类(Non-static Nested Class),后者通常被称为内部类(Inner Class)。 - **静态嵌套类**:此类嵌套类被声明为`static`,因此它们与外部类...
and efficiency. Loops in applications are often mapped onto CGRAs for acceleration, and the mapping of loops onto CGRA is quite a challenging work due to the parallel execution paradigm and ...
Java 中的嵌套类和内部类是指在一个类的内部定义另一个类,这种类称为嵌套类(nested classes)。嵌套类有两种类型:静态嵌套类和非静态嵌套类。静态嵌套类使用很少,非静态嵌套类也即被称作为内部类(inner)。嵌套...
标题中的"PyPI 官网下载 | drf_nested_forms-1.1.5-py3-none-any.whl"指的是在Python的包索引服务(Python Package Index,简称PyPI)上发布的`drf_nested_forms`库的一个特定版本,即1.1.5。这个库是为了解决Django...
它通过自定义序列化器和表单类来实现这一功能,允许你在定义序列化器时定义嵌套关系,这样在提交数据时,会自动进行层次化的验证和处理。 例如,假设你有一个模型`Order`,它包含多个`LineItem`,每个`LineItem`又...
资源来自pypi官网。 资源全名:nested_models-0.1.0-py2-none-any.whl
1. **嵌套顶级类(Nested Top-Level Classes)**:这种内部类类似于普通的类,但它们被定义在另一个类的内部。它们没有访问外部类的实例变量或方法的能力,除非它们是静态的。嵌套顶级类可以通过外部类名访问。 2. ...
资源分类:Python库 所属语言:Python 资源全名:drf_nested_decorator-0.3-py2-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源来自pypi官网。 资源全名:drf_nested_routers-0.92-py2.py3-none-any.whl
为了更好地利用`nested_models`库,开发者需要了解其提供的API接口,包括主要类、方法以及如何初始化和使用它们。此外,阅读库的文档、查看示例代码以及参与社区讨论都是获取更多知识和解决实际问题的好方法。最后,...
内部类和静态嵌套类是这两种语言中用于组织代码结构的重要特性。本篇文章将详细探讨这两个概念,并通过对比Java和Kotlin的差异,帮助初学者理解它们的工作原理和应用场景。 首先,我们来了解Java中的内部类。Java...
`drf_nested_routers`是Python中用于Django REST Framework(DRF)的一个扩展库,它提供了更高级别的路由系统,支持嵌套的路由器结构。这个`.whl`文件是Python的预编译轮子包,它包含了库的源码和其他必要文件,可以...
首先,根据嵌套类定义的位置,可以将嵌套类分为成员嵌套类(Member Nested Classes)、局部嵌套类(Local Nested Classes)和匿名嵌套类(Anonymous Nested Classes)。 成员嵌套类是指作为外部类(Enclosing Class...
1. **嵌套滚动支持**:允许在一个`ScrollView`内部嵌套其他可滚动的组件,如`ListView`、`FlatList`等,所有滚动组件可以协同工作,避免互相冲突。 2. **滚动同步**:当嵌套的子视图滚动时,外层的`...
内部类分为几种类型,包括静态成员类(Static member class)、局部内部类(Local inner class)、匿名内部类(Anonymous inner class)以及嵌套接口(Nested interface)。在本讨论中,我们将主要聚焦于静态成员类...
1. **类设计**:在 `nested-comment-master-dev` 中,开发者通常会定义一个 Comment 类,该类可能包含如 ID、作者、内容、时间戳以及父评论ID等属性。这些属性是构建嵌套评论结构的基础。 2. **数据库模型**:与 ...
嵌套类主要分为两大类:**静态嵌套类**(Static Nested Class)和**内部类**(Inner Class)。这两类之间的主要区别在于它们是否可以访问外部类的实例变量。 ##### 2.1 静态嵌套类 - **定义**:被`static`关键字...