DAY09 Inner class

Source: Internet
Author: User

Inner class: When describing a thing, the inner class of a thing has a transaction, which is described by an inner class.

Access rules for internal classes:
1, the inner class can access the members of the external class directly, including the private,.
Members in an external class can be accessed directly because the inner class holds a reference to an external class, in the form of an external class name. This
2, the outer class must establish an inner class object to access the inner class.

Access format:
1, when the inner class is defined on the member position of the outer class, and is not private, it can be in an external other class,
You can create an inner class object directly.
Format:
The outer class name. internal Class Name Object = External Class object. Inner Class object
Outer.Inner in = new outer (). New Inner ();
2, when the inner class is in the member position, it can be modified by the member modifier,
For example, private: Encapsulate inner classes in the outer class
Static: The inner class has static properties.
When an inner class is modified by static, only static members in the outer class can be accessed directly. Access limitations have occurred

In other classes of external classes, how do you directly access the non-static members of the static inner class?
New Outer.Inner (). function ();

In other external classes, how do you directly access static members of static internal classes?
Outer.inner.function ();

Note: When a static member is defined in the inner class, the inner class must be static,
When a static method in an external class accesses an inner class, the inner class must also be static.

classouter{intX=3; Static classInter {Static voidfunction () {SYSTEM.OUT.PRINTLN ("Inner;" +outer. This. x); }    }    voidMethod () {Inter in=NewInter;    In.function (); }}classTest { Public Static voidMain (string[] args) {outer out=Newouter ();        Out.method (); //direct access to members within the inner class;//Outer.Inner in = new outer (). New Inner (); //in.function ();    }}

DAY09 Inner class

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.