Chapter 1 internal class, Chapter 2
- Internal class: The class defined in the class. The outer class is called the external class and the peripheral class.
- P191 in the book, if you want to create an internal class object anywhere outside of the non-static method of the external class, it must be like in the main method (static method, specify the object type: OuterClassName. innerClassName. (InnerClassName can be directly used in external non-static methods)
- As an example in the book (10.2), the internal class can directly access all the members (methods and fields) of its peripheral class, just as the internal class itself owns them, this brings great convenience. (However, in the main function (static function of the peripheral class), internal class objects cannot access members of the external class)
- Internal classes automatically have access to all members of their peripheral classesP192. How is this done? When an internal class object is created for a peripheral class object, the internal class object will secretly capture a reference pointing to that peripheral class object. Then,
- Iterator Design ModeP192: External classes store data and there are methods that generate internal class objects. Private internal classes implement the Selector interface to access data as an iterator.
- Use"OuTerClassName. This"(P193):This syntax is used to generate references to external class objects that contain the internal class.
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 */// :~View Code
- Use"OuterClassObject Reference. new": To create an internal class object, you must specify the reference of the external class object instead of "new OuterClassName. InnerClassName ();".
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 (); // The dn already specifies the external class, so no (NOR) dn is required. new DotNew. inner (); 18 19 //! DotNew. Inner dn2= new DotNew. Inner (); 20 21} 22 23 }///:~View Code
- Anonymous internal class(P197 ):Define a class in a function. The class name is not displayed and can be used at one time. Generally, there are parent classes and parent interfaces. Functions of anonymous internal classes can also be used in addition to definitions. The end of the anonymous internal class definition is separated.
- A static function is used as a function similar to a constructor.(P198), there is a piece of code in the static function to implement the instance initialization function.
- In the book (198-199), the parameter must be final if it is used inside an anonymous internal class, if final is removed, no error is reported)
- Instance Initialization: What is "instance initialization"? Because the Class Name of the anonymous internal class is omitted, there is no constructor. Instead of the constructor, use the "instance initialization" function, as shown in the following example: Parcel10.java, the code that implements the "instance initialization" function must be enclosed by braces.
- Anonymous internal classes can both extend classes and implement interfaces, but they cannot be both. You cannot implement multiple interfaces.
- Use "Factory method design pattern" of "anonymous internal class" to rewriteInterfaceS/Games. java
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 }View Code