java-Inner class

Source: Internet
Author: User

java-Inner class
an internal class
A class defined in a class, called an inner class, and an anonymous inner class.

two access rules for internal classes
1, the inner class can access members in the outer class directly, including private members.
Direct access is possible because the inner class holds a reference to an external class.
The format is: external class name. This
2, the external class must establish an inner class object to access the inner class

class Test{    privateint3;        class Inner    {        void fun()        {            //可以直接访问私有成员            System.out.println("inner:"+Inner.this.x);        }    }}

three static inner classes
1, Access format:
The outer class name. Internal class Name Variable name = External Class object. Inner Class object

innew Test().new Inner();

2, when the inner class is in the member position, it can be decorated by the member modifier:
Private adornment: Encapsulates inner classes in the outer class
Static: The inner class has static properties, and when the inner class is modified by static, only access to the outer class
Static member, access restrictions appear
In other external classes, how do I access non-static members in static inner classes?

new Test2.Inner().fun();

In other external classes, how do you directly access static members of static inner classes?

Test2.Inner.fun();

3. Note that when a static member is defined in an inner class, the inner class must be static, because only static members can
Access to static

four anonymous inner class
An inner class without a name is called an anonymous inner class, which is often used to simplify the code

principle of Use:
1. Inheriting a class or implementing an interface
2, Format:

new Object(){    publicvoidfun(0    {    }}.fun();

This format requires that the object must be a defined parent class or interface, and that the parent class or connector
The method to override is less than or equal to 2.
The reason for defining an anonymous inner class is to write the method of the parent class or interface.
The format can also be used for argument lists of functions

show(new Object(){    publicvoidfun(0    {    }});

Note: When a function's argument list is an interface object, the anonymous inner class is often passed

Demo:

class TestDemo{static Inter fun(){    returnnew Inter()    {        publicvoidmethod()        {        }    };}}class Test3 {    publicstaticvoidmain(String[] args)    {    //Test.fun():TestDemo中有静态方法fun        //.method方法返回的是一个inter对象,然后才能调用method方法        TestDemo.fun().method();     } }

Copyright NOTICE: Welcome to communicate the error of the article, must humbly accept, QQ872785786

java-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.