"Java Core Technology (Volume I)" Reading notes--the sixth chapter: inner class

Source: Internet
Author: User

1. The concept of an inner class?

Classes in class

2. Why use internal classes?

Methods of the inner class can access the instance domain of the external class

Inner classes in the same package as the external class implement a hidden

Anonymous inner classes are very handy when you want to define a callback function but don't want to write a lot of code.

3. Internal class Syntax details (general)

1. Inner and outer classes are relationships between classes, not relationships between objects

Although the Timeprinter class is inside the Talkingclock class, this does not mean that each Talkingclock object has a Timeprinter instance domain. Because the Timeprinter object is constructed by the Start method, that is, if the Talkingclock object does not call the Start method, it does not own a Timeprinter instance domain.

2. The inner class can access both its own data domain and the instance domain of the external class object that created it

3. Implicit references to inner classes: pointing to external class objects (compiler Responsible)

if (beep)--The IF (outer.beep)//outer is used only as a schematic, the inside of the compiler is certainly not outer

An implicit reference to an external class is set in the constructor. The compiler modifies all constructors for the inner class, adding a parameter that references the external class. Even if you do not define a constructor for an inner class, the compiler generates a default constructor for the inner class. such as:Publictimeprinter (talkingclock clock) {outer = clock;}

When an inner class object is created in the Start method, the compiler passes the this reference to the current constructor

Actionlistenerlistener = new Timeprinter (this);

4. Declare the inner class as private, to hide it externally, only the outer class can construct the inner class object

5. Referencing an inner class outside the scope of the outer class

Outerclass.innerclass

4. Special syntax for inner classes: how to correctly use external class reference parameters

A) The inner class accesses the external class instance domain

if (beep)-"if" (TalkingClock.this.beep);

b) Inner class constructor

ActionListener listener = this.new timeprinter ();

/* Inner class constructed by this method, its implicit reference to the same object as this point */

c) Set the external class reference to another object by displaying the name

The following code, in the case of Timeprinter public, any external class can construct a timeprinter

Talkingclockjabber = new Talkingclock (+, True);

Talkingclock.timeprinter listener = Jabber.newtimeprinter ();

Recorded from Http://blog.csdn.net/Reese1995/article/details/77848502?ref=myread

"Java Core Technology (Volume I)" Reading notes--the sixth chapter: inner class

Related Article

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.