Java-InnerClass-06

Source: Internet
Author: User
/** // * Reference the peripheral Class Object: The peripheral class name is followed by a period and this keyword. eg: In the sequence class. in sselector. this, any method of the internal class can obtain the stored reference pointing to the peripheral class sequence.
* To generate internal class objects of an object, you must provide a reference in new to point to the peripheral class object. eg: Contents con = parcel. new contents (); equivalent to contents con = parcel. getcontents ();
* Creating an internal class object cannot take it for granted that it is okay to add the name of the peripheral class parcel. Instead, you must use the object of this peripheral class to create an internal class object. this is because the object of this internal class will be completely connected to the object of the peripheral class that created it (except for the nested class ).
*/

/** // * Why do I need an internal class?
* The internal class inherits from a class or implements an interface. The Code operation of the internal class creates its peripheral Class Object. Therefore, it can be considered that the internal class provides a window for its peripheral class.
* Using a peripheral class to implement an interface is not always convenient to use the interface (otherwise, the internal class does not need to exist). The most attractive reason for the internal class is: each internal class can inherit one (Interface) implementation independently, so no matter whether the peripheral class has inherited one (Interface) implementation or not, it has no impact on the internal class.
* Internal classes provide the ability to inherit multiple classes (non-abstract or abstract) (internal classes allow inheritance of multiple non-interface types ), therefore, the internal class makes the multi-inheritance solution more complete (the peripheral class can only inherit multiple interfaces and one super class ).
*
* For Multiple Interfaces: when a problem occurs, the problem itself usually provides some guidance to tell you whether a single class or an internal class should be used;
* For multiple base classes: Only internal classes can be used for multi-inheritance.
*/

/** // * Closure and callback
* Closure is a callable object that records some information from the scope of its creation. through this definition, we can see that the internal class is an object-oriented closure, because it not only contains information about the peripheral Class Object (the scope of the internal class to be created, it also automatically has a reference pointing to this peripheral Class Object. Within this scope, the internal class has the right to operate on all members (including private members ).
* Callback: Through callback, an object can carry some information that allows it to call the initial object at a certain time later.
* Providing closures through internal classes is a perfect solution.
*/
Interface myincrement1. .. {void increment ();}
Class myincrement2 ...{
Void increment ()... {system. Out. println ("class myincrement's operation: increment ()");}
Static void F (myincrement2 mi)... {mi. increment ();}
}

Class callee1 implements myincrement1 ...{
Private int I;
Public void increment ()...{
System. Out. println ("callee1: interface myincrement1's operation: increment ()" + (I ++ ));
}
}
Class callee2 extends myincrement2 ...{
Private int I;
Private void incr ()... {system. Out. println ("callee2: interface myincrement1's operation: increment ()" + (I ++ ));}
Private class closure implements myincrement1 ...{
Public void increment ()... {incr ();}
}
Public myincrement1 getcallbackreference ()... {return new closure ();}
}
Class caller ...{
Private myincrement1 callbackreference;
Caller (myincrement1 mi)... {callbackreference = mi ;}
Void go ()... {callbackreference. increment ();}
}
Class callbacks ...{
Public static void main (string [] ARGs )...{
Callee1 C1 = new callee1 ();
Callee2 C2 = new callee2 ();
Myincrement2.f (C2 );
Caller caller1 = new caller (C1 );
Caller caller2 = new caller (c2.getcallbackreference ());
Caller1.go ();
Caller2.go ();
Caller1.go ();
Caller2.go ();
}
}

/** // The preceding example further reveals the differences between an interface implemented by the peripheral class and an internal class. callee1 is a simple inheritance interface. callee2 inherits from myincrement2, and class myincrement2 already has an increment () method, which is completely irrelevant to the increment () method in myincrement1 interface.
* If callee2 inherits myincrement2, it cannot overwrite the increment () method of myincrement2 for the purpose of myincrement1. Therefore, it can only implement myincrement1.
* In callee2, except getcallbackreference (), other members are private. to establish any connection with the external world, the interface myincrement1 is required.
* The internal class closure implements myincrement1 to provide a "Hook" (and a secure hook) that returns callee2 ). no matter who gets the reference of this myincrement1, only the increment () method can be called. In addition, there are no other functions.
* The caller constructor requires a reference of myincrement1 as the parameter. Then, at a later time, the caller object can use this reference to call back the callee class.
* The value of callback lies in its flexibility. It dynamically determines the method to be called during runtime.
*/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.