---------转载 builder.com.cn
2 Member class(成员类)
一个静态成员类,若去掉“static”关键字,就成为成员类。如下示例代码,Inner1/Inner2/Inner3/Inner4就是Outer的四个成员类
public
class
Outer {
// just like instance method, member class has public/private/default access privilege levels
private int data;
// access privilege level: public
public class Inner1 {
private int data;
private int data1;
public Inner1() {
// member class can access its outer class' instance field directly
data1 = 1 ;
// itself data field
data = 1 ;
// its outer class instance field
Outer. this .data = 1 ;
}
}
// access privilege level: default
class Inner2 {
// can not define static filed, method, class in member class
// static int j = 1;
// but, "static final" compound is allowed
static final int CONSTANT = 1 ;
}
// access privilege level: private
private class Inner3 {
public class Inner4
}
// in fact, Inner5 is not a member class but a static member class
interface Inner5
private static void staticMethod() {
// can not create a member class instance directly in outer class' static method
// Inner1 inner1 = new Inner1();
}
private void instanceMethod() {
// can create a member class instance in outer class' instance method
Inner1 inner1 = new Inner1();
}
}
class Test {
public Test() {
// cannot create member class instance directly in class other than outer class
// Outer.Inner2 inner2 = new Outer.Inner2();
// create a member class instance outside it's outer class
Outer outer = new Outer();
Outer.Inner1 inner1 = outer. new Inner1();
}
}
// just like instance method, member class has public/private/default access privilege levels
private int data;
// access privilege level: public
public class Inner1 {
private int data;
private int data1;
public Inner1() {
// member class can access its outer class' instance field directly
data1 = 1 ;
// itself data field
data = 1 ;
// its outer class instance field
Outer. this .data = 1 ;
}
}
// access privilege level: default
class Inner2 {
// can not define static filed, method, class in member class
// static int j = 1;
// but, "static final" compound is allowed
static final int CONSTANT = 1 ;
}
// access privilege level: private
private class Inner3 {
public class Inner4
}
// in fact, Inner5 is not a member class but a static member class
interface Inner5
private static void staticMethod() {
// can not create a member class instance directly in outer class' static method
// Inner1 inner1 = new Inner1();
}
private void instanceMethod() {
// can create a member class instance in outer class' instance method
Inner1 inner1 = new Inner1();
}
}
class Test {
public Test() {
// cannot create member class instance directly in class other than outer class
// Outer.Inner2 inner2 = new Outer.Inner2();
// create a member class instance outside it's outer class
Outer outer = new Outer();
Outer.Inner1 inner1 = outer. new Inner1();
}
}
2.1 成员类特性
· 类似于外部类的实例函数,成员类有public/private/default权限修饰符
· 一个成员类实例必然所属一个外部类实例,成员类可访问外部类的任一个实例字段和实例函数。
2.2 成员类约束
成员类不能与外部类重名
不能在成员类中定义static字段、方法和类(static final形式的常量定义除外)。因为一个成员类实例必然与一个外部类实例关联,这个static定义完全可以移到其外部类中去
成员类不能是接口(interface)。因为成员类必须能被某个外部类实例实例化,而接口是不能实例化的。事实上,如示例代码所示,如果你以成员类的形式定义一个接口,该接口实际上是一个静态成员类,static关键字对inner interface是内含(implicit)的。
2.3 新增语法
一个成员类实例必然所属于其外部类的一个实例,那么如何在成员类内部获得其所属外部类实例呢?如示例代码所示,采用“OuterClass.this”的形式。
2.4 指定内部类实例所属的外部类实例
内部类实例可在其外部类的实例方法中创建,此新创建内部类实例所属的外
部类实例自然就是创建它的外部类实例方法对应的外部类实例。
另外,如示例代码所示,对于给定的一个外部类实例outerClass,可以直接创建其内部类实例,语法形式为:
OuterClass.InnerClass innerClass = outerClass.new InnerClass();
|
2.5 什么时候使用成员类
成员类的显著特性就是成员类能访问它的外部类实例的任意字段与方法。方便一个类对外提供一个公共接口的实现是成员类的典型应用。
以JDK Collection类库为例,每种Collection类必须提供一个与其对应的Iterator实现以便客户端能以统一的方式遍历任一Collection实例。每种Collection类的Iterator实现就被定义为该Collection类的成员类。例如JDK中AbstractList类的代码片断:
public
abstract
class
AbstractList
<
E
>
extends
AbstractCollection
<
E
>
implements
List
<
E
>
{
private class Itr implements Iterator < E > {
………;
}
public Iterator < E > iterator() {
return new Itr();
}
}
private class Itr implements Iterator < E > {
………;
}
public Iterator < E > iterator() {
return new Itr();
}
}
因为定义在AbstractList中的Itr可访问AbstractList中的任意字段和方法,所以很方便实现Iterator,无需AbstractList对外暴露更多的接口。
试想,如果没有成员类机制,只有在AbastractList源码之外定义一个实现Iterator的类Itr,该类有一个AbstractList实例成员list,为了Itr能获取list的内部信息以便实现遍历,AbstractList必然要向Itr开放额外的访问接口。