1 public class DoThis {
2 void f(){System.out.println("DoThis.f()");}
3 public class Inner{
4 public DoThis getOuter(){
5 return DoThis.this;
6 }
7 }
8 public Inner getInner(){
9 return new Inner();
10 }
11 public static void main(String[] args) {
12 DoThis dThis = new DoThis();
13 DoThis.Inner dThisInner = dThis.getInner();
14 dThisInner.getOuter().f();
15 }
16 }
1 public class DoNew {
2 public class Inner{};
3 public static void main(String[] args) {
4 DoNew doNew = new DoNew();
5 DoNew.Inner dInner = doNew.new Inner();
6 }
7 }
1 class Parcel4{
2 private class PContents implements Contents {
3 private int i = 11;
4 @Override
5 public int value() { return i; }
6 }
7 protected class PDestination implements Destination {
8 private String label;
9 private PDestination(String whereTo) { label = whereTo; }
10 @Override
11 public String readLabel() { return label; }
12 }
13 public Destination destination(String s){ return new PDestination(s); }
14 public Contents contents(){ return new PContents(); }
15 Parcel4.PContents t;//PContents是private,只能在Parcel4內部訪問
16 }
17 public class TestParcel {
18 public static void main(String[] args) {
19 Parcel4 p = new Parcel4();
20 Contents contents = p.contents();
21 Destination destination = p.destination("Tasmania");
22 //Parcel4.PContents pc = p.new PContents();//PContents是private,只能在Parcel4內部訪問,此處報錯
23 }
24 }
1、一個定義在方法中的類
1 public class Parcel5 {
2 public Destination destination(String s){
3 class PDestination implements Destination{
4 private String label;
5 private PDestination(String whereTo){
6 label = whereTo;
7 }
8 @Override
9 public String readLabel() { return label; }
10 }
11 return new PDestination(s);
12 }
13 public static void main(String[] args) {
14 Parcel5 p = new Parcel5();
15 Destination destination = p.destination("Tasmania");
16 }
17 }
2、一個定義在作用域中的類,作用域在方法的內部
1 public class Parcel6 {
2 private void internalTracking(boolean b) {
3 if (b){
4 class TrackingSlip {
5 private String id;
6 public TrackingSlip(String s) {
7 id = s;
8 }
9 String getSlip(){ return id;}
10 }
11 TrackingSlip ts = new TrackingSlip("slip");
12 String s = ts.getSlip();
13 }
14 //Can't use it here!Out of scope:
15 //TrackingSlip ts = new TrackingSlip("x");
16 }
17 public void track(){internalTracking(true);}
18 public static void main(String[] args) {
19 Parcel6 p = new Parcel6();
20 p.track();
21 }
22 }
3、一個實現了接口的匿名類
4、一個匿名類,擴展了有非默認構造器的類
5、一個匿名類,執行字段初始化
6、一個匿名類,通過實例初始化實現構造(匿名類沒有構造器)
1 public interface ClassInInterface {
2 void howdy();
3 class Test implements ClassInInterface{
4 @Override
5 public void howdy() { System.out.println("Howdy"); }
6 public static void main(String[] args){
7 new Test().howdy();
8 }
9 }
10 }
1 class A{
2 private void f(){}
3 class B {
4 private void g(){}
5 public class C {
6 void h() {
7 g();
8 f();
9 }
10 }
11 }
12 }
13 public class MultiNestingAccess {
14 public static void main(String[] args) {
15 A a = new A();
16 A.B ab = a.new B();
17 A.B.C abc =ab.new C();
18 abc.h();
19 }
20 }
1 package com.helei.innerclasses;
2 interface Incrementable {
3 void increment();
4 }
5 class Callee1 implements Incrementable {
6 private int i = 0;
7 @Override
8 public void increment() {
9 i++;
10 System.out.println(i);;
11 }
12 }
13 class MyIncrement {
14 public void increment() { System.out.println("Other operation");}
15 static void f(MyIncrement mi) {mi.increment();}
16 }
17 class Callee2 extends MyIncrement {
18 private int i = 0;
19 public void increment() {
20 super.increment();
21 i++;
22 System.out.println(i);
23 }
24 private class Closure implements Incrementable {
25 public void increment() {
26 Callee2.this.increment();
27 }
28 }
29 Incrementable getCallbackReference() {
30 return new Closure();
31 }
32 }
33 class Caller {
34 private Incrementable callbackReference;
35 Caller(Incrementable cbh){callbackReference = cbh;}
36 void go() {callbackReference.increment();}
37 }
38 public class Callbacks {
39 public static void main(String[] args) {
40 Callee1 c1 = new Callee1();
41 Callee2 c2 = new Callee2();
42 MyIncrement.f(c2);
43 Caller caller1 = new Caller(c1);
44 Caller caller2 = new Caller(c2.getCallbackReference());
45 caller1.go();
46 caller1.go();
47 caller2.go();
48 caller2.go();
49 }
50 }