内部类,嵌套类(Nested, Inner, Member, and Top-Level Classes)
程序员文章站
2022-07-12 11:02:27
...
要点:
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