1 public class DotThis { 2 3 void f() { 4 5 System.out.println("DotThis.f()"); 6 7 } 8 9 public class Inner { 10 11 public DotThis outer() { 12 13 f(); 14 15 return DotThis.this; 16 17 // A plain "this" would be Inner‘s "this" 18 19 } 20 21 } 22 23 public Inner inner() { 24 25 return new Inner(); 26 27 } 28 29 public static void main(String[] args) { 30 31 DotThis dt = new DotThis(); 32 33 DotThis.Inner dti = dt.inner(); 34 35 dti.outer().f(); 36 37 } 38 39 } /* 40 41 * Output: DotThis.f() 42 43 */// :~
Eg:
1 public class DotNew { 2 3 public class Inner { 4 5 public Inner(){ 6 7 System.out.println("Inner class constructor."); 8 9 } 10 11 } 12 13 public static void main(String[] args) { 14 15 DotNew dn = new DotNew(); 16 17 DotNew.Inner dni = dn.new Inner(); //dn已经指明外部类,故不需(也不能)dn.new DotNew.Inner(); 18 19 //!DotNew.Inner dni2 = new DotNew.Inner(); 20 21 } 22 23 } ///:~
1 package exercise; 2 import static net.mindview.util.Print.*; 3 4 interface Game { 5 boolean move(); 6 } 7 8 interface GameFactory { 9 Game getGame(); 10 } 11 12 class Checkers implements Game{ 13 private int moves = 0; 14 private static final int MOVES = 3; 15 public boolean move(){ 16 print("Checkers move " + moves); 17 return ++moves != MOVES; 18 } 19 public static GameFactory factory = new GameFactory(){ 20 public Game getGame(){ 21 return new Checkers(); 22 } 23 }; 24 } 25 26 27 public class Common { 28 29 public static void serviceConsumer(GameFactory fact){ 30 Game g = fact.getGame(); 31 while(g.move()); 32 } 33 34 public static void main(String[] args) { 35 serviceConsumer(Checkers.factory); 36 } 37 38 }
原文:http://www.cnblogs.com/HITSZ/p/6411300.html