标签:
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 { ... } }
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.)
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.
理解:1.有利于从逻辑上将类聚合在一起:如果一个类只被另一个类所用,我们就可以将这个嵌入其中,这样更有利于包的线性化。2.增加了封装性:如果类B需要访问类A的成员,那么A的成员就不能声明为私有的。但是将B作为A的内部类的时候,那么B就可以访问A中的私有成员(有利于A的封装)。B也与外部隔离开来。3.使代码更加可读与可维护:将”小“类封装在”大“类中,更便于使用
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.
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();
理解:就像类成员变量和方法,内部类可以看为外部类的一部分。像静态类方法一样,静态内部类无法访问外部类的成员变量和方法:只有通过对象引用来访问。1.访问静态内部类方法:(外部类名.内部类名)2.创建内部类实例:外部类名.jing
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.
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 methodmethodInFirstLevel
:
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
Serialization of inner classes, including local and anonymous classes, is strongly discouraged. When the Java compiler compiles certain constructs, such as inner classes, it creates synthetic constructs; these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code. Synthetic constructs enable Java compilers to implement new Java language features without changes to the JVM. However, synthetic constructs can vary among different Java compiler implementations, which means that .class
files can vary among different implementations as well. Consequently, you may have compatibility issues if you serialize an inner class and then deserialize it with a different JRE implementation. See the section Implicit and Synthetic Parameters in the section Obtaining Names of Method Parameters for more information about the synthetic constructs generated when an inner class is compiled.
标签:
原文地址:http://www.cnblogs.com/Guoyutian/p/5075347.html