内部类可以直接访问外部类的成员,包括private成员,但内部类的成员却不能被外部类直接访问(很好理解,和类中方法体规则一样).
在内部类对象保存了一个对外部类对象的引用.当内部类的成员方法中访问某一变量时,如果在该方法和内部类中都没有定义过这个变量,内部类中对this的引用会被传递给那个外部类对象的引用.

/** *//************************简单举例*************************/

class Outer...{
private int outer_i = 100;

public void test()...{
Inner inner = new Inner();
inner.display();
}

class Inner...{

public void display()...{
System.out.println("outer_i = " + outer_i);

/**//*先在display()方法中找,然后在Inner类中找,再在Outer类中找,直至找到outer_i(很好理解,变量覆盖也是如此实现的).*/
}
}

public static void main(String[] args)...{
Outer outer = new Outer();
outer.test();
}
}

/**//*外部类生成内部类对象的方法*/

class TestOuter...{

public static void main(String[] args) ...{
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.display();
}
}

/** *//************************class Outer的改进*************************/

class Outer_opt...{
int outer_i = 100;

class Inner...{

public void display()...{
System.out.println("outer_i = " + outer_i);
}
}

/**//*在外部类设置一个方法,该方法返回一个指向内部类的引用.这种模式很常见,有利于代码维护.*/

public Inner getInner()...{
return new Inner();
}

public void test()...{
Inner inner = getInner();
inner.display();
}

public static void main(String[] args) ...{
Outer outer = new Outer();
outer.test();
}
}


/** *//************************简单举例*************************/

/**//*内部类允许你把一些逻辑相关的类组织在一起,比如下例*/

class Parcel...{

class Contents...{
private int value;

Contents(int value)...{
this.value = value;
}

public int readValue()...{
return value;
}
}

class Destination...{
private String label;

Destination(String toWhere)...{
this.label = toWhere;
}

public String readLabel()...{
return label;
}
}

public void ship(int value,String destination)...{
Contents con = new Contents(value);
Destination des = new Destination(destination);
System.out.println(con.readValue());
System.out.println(des.readLabel());
}

public static void main(String[] args) ...{
Parcel parcel = new Parcel();
parcel.ship(15,"ShangHai");
}
}


/** *//************************class Parcel的改进*************************/

class Parcel_opt...{

class Contents...{
private int value;

Contents(int value)...{
this.value = value;
}

public int readValue()...{
return value;
}
}

class Destination...{
private String label;

Destination(String toWhere)...{
label = toWhere;
}

public String readLabel()...{
return label;
}
}

public Contents getContents(int value)...{
return new Contents(value);
}

public Destination getDestination(String label)...{
return new Destination(label);
}

/**//*在外部类设置一个方法,该方法返回一个指向内部类的引用.这种模式很常见,有利于代码维护.*/

public void ship(int value,String label)...{
Contents con = getContents(value);//类的内部调用非静态方法不需要object.method(),其他类中需要object.method()
Destination des = getDestination(label);
System.out.println(con.readValue());
System.out.println(des.readLabel());
}

public static void main(String[] args) ...{
Parcel_opt parcel_1 = new Parcel_opt();
parcel_1.ship(25,"Beijing");
Parcel_opt parcel_2 = new Parcel_opt();
parcel_2.ship(30,"TaiYuan");
Parcel_opt.Contents contents = parcel_2.getContents(40);
Parcel_opt.Destination destination = parcel_2.getDestination("DaLian");

/**//*如果想从外部类的非静态方法之外(静态方法内or方法体外)的任意位置创建内部类的对象,那么必须具体指明这个对象的类型:OuterClassName.InnerClassName.*/
}
}
在内部类对象保存了一个对外部类对象的引用.当内部类的成员方法中访问某一变量时,如果在该方法和内部类中都没有定义过这个变量,内部类中对this的引用会被传递给那个外部类对象的引用.






































































































































































