1. Creating an inner class object
1.1 Create an Inner class object (equivalent to the new inner class object) Outer class object. New inner Class ().
1.2 Creating an inner class object One method is that the perimeter class has a method that returns an inner class object.
1.3 You cannot create an inner class object without a perimeter class object because the inner class object is secretly connected to the perimeter class object.
1.4 If a nested class (Static inner Class) can be created without a perimeter class object reference. Outer.Inner in = Outer.Inner ();
2. Create an inner class reference in
- Outer.Inner in, whether in any other class or within the enclosing class.
- Inner class creates a perimeter class object reference out:Outer.this out
3. Anonymous inner class
Public Test F () { returnnew Test () { // from Test () to; is an anonymous inner class, equivalent to a class, and inherits the Test // class data member // New test () is the base class of the anonymous inner class // class Method }; }
Result: A call to F () creates an anonymous object that inherits from test and automatically transitions up to the test reference and returns the reference.
3.1 Anonymous inner class if you need an object defined by a perimeter class, the object's parameters must be final.
Public Final String A1) { returnnew Test () { = A1; }; }
3.2 Anonymous inner class without a name there is no constructor, and if you want to perform initialization behavior when creating an anonymous object, like a constructor, you can do the constructor behavior by initializing the instance.
3.3 Anonymous inner class implements interface, only one interface can be implemented
class a{ new Animal () { @Override publicvoid Fly () { // TODO auto-generated method stub } }; }
3.4 The anonymous inner class extends the base class, and the anonymous extension class either extends the base class or implements the interface, as opposed to the classes inherited by extends, and not both.
4. Nested classes
- Static inner class, no connection between internal and external classes objects. The static class is a separate existence, just put it in a class.
4.1. Nested classes cannot access non-static member objects of the perimeter class because they are static
4.2. Ordinary inner classes cannot have static fields and static data members, nor can they nest an inner class, but nested classes are available.
5. Internal classes of interfaces
- Because any class in the interface is automatically public and static, there is no need to write static and nested
6. Why internal classes are required
- Internal classes can resolve partial multiple inheritance problems
- An inner class can inherit the implementation interface independently, regardless of whether the peripheral class inherits the interface.
- An object of the inner class operation perimeter class that provides a window into the perimeter class
7. Closures
Closures are objects that record objects from the scope in which they were created.
8. What is a callback.
- A sends a message to B, and when B finishes processing, the method that invokes a executes the result of the processing.
- 19. The flexibility of callbacks: Dynamically deciding what method to invoke at run time .
9. Inheriting internal classes
class B { class A { }}classextends B.A { //C inherits only internal classes C (b b) { // Create a C object requires calling A's constructor, but the constructor that calls a must have a reference B.Super(); // The constructor for C passes in a reference to B, and then calls the constructor of base class A } }
Note: B.super () represents invoking the base class constructor of Class C with a B reference, instead of invoking the constructor of the B base class
9.1 When subclasses overwrite the inner class of a base class, the two inner classes are two separate inner classes, because the namespace differs from the method override.
9.2 Inner classes can inherit inner classes, as the inner class of the base class must be Outer.Inner
10. Local Inner class
-------later Supplement
Knowledge points
1. Multi-layer nesting, no matter how many layers are nested they are always in a class, and the members within the same class can access each other (but beware of static), even if it is private
2. Application framework: A class or set of classes that are used to solve certain problems.
3. The control framework is a special application framework.
Java Programming thought note tenth inner class