---转builder.com.cn
本文关键词: Inner Class JAVA
简单的说,内部(inner)类指那些类定义代码被置于其它类定义中的类;而对于一般的、类定义代码不嵌套在其它类定义中的类,称为顶层(top-level)类。对于一个内部类,包含其定义代码的类称为它的外部(outer)类。
1 Static member class(静态成员类)
类声明中包含“static”关键字的内部类。如以下示例代码,
Inner1/Inner2/Inner3/Inner4就是Outer的四个静态成员类。静态成员类的使用方式与一般顶层类的使用方式基本相同。
public
class
Outer{
// just like static method, static member class has public/private/default access privilege levels
// access privilege level: public
public static class Inner1 {
public Inner1() {
// Static member inner class can access static method of outer class
staticMethod();
// Compile error: static member inner class can not access instance method of outer class
// instanceMethod();
}
}
// access privilege level: default
static class Inner2
// access privilege level: private
private static class Inner3 {
// define a nested inner class in another inner class
public static class Inner4
}
private static void staticMethod() {
// cannot define an inner class in a method
/* public static class Inner4() */
}
private void instanceMethod() {
// private static member class can be accessed only in its outer class definition scope
Inner3 inner3 = new Inner3();
// how to use nested inner class
Inner3.Inner4 inner4 = new Inner3.Inner4();
}
}
class Test {
Outer.Inner1 inner1 = new Outer.Inner1();
// Test and Outer are in the same package, so Inner2 can be accessed here
Outer.Inner2 inner2 = new Outer.Inner2();
// Compile error: Inner3 cannot be accessed here
// Outer.Inner3 inner3 = new Outer.Inner3();
}
// just like static method, static member class has public/private/default access privilege levels
// access privilege level: public
public static class Inner1 {
public Inner1() {
// Static member inner class can access static method of outer class
staticMethod();
// Compile error: static member inner class can not access instance method of outer class
// instanceMethod();
}
}
// access privilege level: default
static class Inner2
// access privilege level: private
private static class Inner3 {
// define a nested inner class in another inner class
public static class Inner4
}
private static void staticMethod() {
// cannot define an inner class in a method
/* public static class Inner4() */
}
private void instanceMethod() {
// private static member class can be accessed only in its outer class definition scope
Inner3 inner3 = new Inner3();
// how to use nested inner class
Inner3.Inner4 inner4 = new Inner3.Inner4();
}
}
class Test {
Outer.Inner1 inner1 = new Outer.Inner1();
// Test and Outer are in the same package, so Inner2 can be accessed here
Outer.Inner2 inner2 = new Outer.Inner2();
// Compile error: Inner3 cannot be accessed here
// Outer.Inner3 inner3 = new Outer.Inner3();
}
1.1 静态成员类特性
静态成员类可访问外部类的任一静态字段或静态方法
像静态方法或静态字段一样,静态成员类有public/private/default权限修饰符
1.2 静态成员类约束
静态成员类不能与外部类重名
像外部类的静态方法一样,不能直接访问外部类的实例字段和实例方法
静态成员类只能定义于外部类的顶层代码或外部类其它静态成员类的顶层代码中(嵌套定义);不能定义于外部类的某个函数中。
1.3 新增语法
如示例代码所示,可以以“OuterClass.InnerClass”的方式来引用某个内部类。
1.4 什么时候使用静态成员类
B为A的辅助类,且只为A所用时,可将B定义为A的静态成员类。例如JDK中的LinkedList类就有Entry静态成员类:
public
class
LinkedList
<
E
>
extends
AbstractSequentialList
<
E
>
…;
private static class Entry < E > {
E element;
Entry < E > next;
Entry < E > previous;
Entry(E element, Entry < E > next, Entry < E > previous) {
this .element = element;
this .next = next;
this .previous = previous;
}
}
…;
}
…;
private static class Entry < E > {
E element;
Entry < E > next;
Entry < E > previous;
Entry(E element, Entry < E > next, Entry < E > previous) {
this .element = element;
this .next = next;
this .previous = previous;
}
}
…;
}
显然,Entry用来表示LinkedList中的一个结点,只被LinkedList自身使用。