10.1 创建内部类
public class Parcel2 {
class Contents {
}
class Destination {
}
public Destination to ( ) {
return new Destination ( ) ;
}
public Contents contents ( ) {
return new Contents ( ) ;
}
public static void main ( String[ ] args) {
Parcel2 p = new Parcel2 ( ) ;
Parcel2. Contents c = p. contents ( ) ;
Parcel2. Destination d = p. to ( ) ;
}
}
10.2 链接到外部类
生成一个内部类对象时,此对象与制造它的外部类对象就有了一种联系,它能访问外部类对象的所有成员
下面这个类使用了迭代器设计模式 将一个迭代器作为一个固定长度数组类的迭代器
interface Selector {
boolean end ( ) ;
Object current ( ) ;
void next ( ) ;
}
public class Sequence {
private Object[ ] items;
private int next = 0 ;
public Sequence ( int size) {
items = new Object [ size] ; }
public void add ( Object x) {
if ( next < item. length)
item[ next++ ] = x;
}
private class SequenceSelector implements Selector {
private int i = 0 ;
public boolean end ( ) {
return i == items. length; }
public Object current ( ) {
return items[ i] ; }
public void next ( ) {
if ( i < items. length) i++ ; }
}
public SequenceSelector selector ( ) {
return new SequenceSelector ( ) ;
}
public static void main ( String[ ] args) {
Sequence sequence = new Sequence ( 10 ) ;
for ( int i = 0 ; i < 10 ; i++ )
sequence. add ( Integer. toString ( i) ) ;
Selector selector = sequence. selector ( ) ;
while ( ! selector. end (