Java Class (ii): inner class

Source: Internet
Author: User
Tags static class

If a class is to be declared static, there is only one case, the static inner class. If the external class is declared static, the program will not compile. After a survey, the individual summed up 3 points about inner class and static inner Class (commonly known as: inline Class)

1. Ordinary inner class: Note the method called "external class cannot be abstract class and interface"

Outer classes class out {    private int-age = n;         Inner class    ' {public        void ' print () {System.out.println (age)}} ' public        class '    Demo {    public static void Main (string[] args) {        out.in in = new Out (). New in ();        In.print ();    }}

2. Anonymous inner class: note structure ======> Note this parent constructor can be an abstract class

New parent class Constructor (parameter list) | Implements interface ()        {         ///Anonymous Inner class class body part        }  

Anonymous internal classes are called in two different ways

Package Lianxi;public class Lianxi {public    static void Main (string[] args) {    /*====>  This construction method can implement a method implementation that is not in the parent class called     * New Dongwu () {public    void Xiuxi () {    System.out.println ("Look at this Place");    };    public void Xiuxi1 () {    System.out.println ("Look at this Place");}    ;    }. XIUXI1 (); *    /Dongwu D = new Dongwu () {        // This method can only be overridden by a method of the external class  or the call will be error public    void Xiuxi ( {    System.out.println ("Look at this place")    ;}; D.xiuxi ();    }} Abstract class Dongwu{public static int age = 10;public void Chi () {System.out.println ("eat Meat");} abstract void Xiuxi ();} Class Dog extends Dongwu{public void Xiuxi () {System.out.println ("Entertainment Project" +dongwu.age);} public void Wan () {System.out.println ("Entertainment Play Project" +dongwu.age);}}

3. Static inner class =====> Note call method

Class Out {    private static int = n;         Static class in {public        void print () {System.out.println (age)}} ' public '    class Demo {public    stat IC void Main (string[] args) {        out.in in = new out.in ();        In.print ();    }}

4. Method inner Class (local inner Class) = = = "Understanding can be said to be infrequently used

Method Inner class cannot be decorated with a restriction (such as public)

Class out {    private int: age = n;     public void Print (final int ×) {        class in {public            void Inprint () {                System.out.println (x);                System.out.println (age);            }        }        New in (). Inprint ();    }} public class Demo {public    static void Main (string[] args) {        out-out = new Out ();        Out. Print (3);}    }

5.

In the above code, we move the inner class into the method of the outer class, and then regenerate it into an inner class object in the method of the outer class to invoke the inner class method

If we need to pass in a parameter to a method in the outer class at this point, then the method parameter of the outer classes must use the final definition

As for final here there is no special meaning, just a form of expression " this place I have not met, and later I have to say," I tested a few times, but not final "

6. Internal incoming values also define a method for the inner class to indirectly change other parameters

New ActionListener () {            private String innerstr;            @Override public            void actionperformed (ActionEvent arg0) {                             jl.settext (INNERSTR);            }            Public ActionListener Accept (String str) {                this.innerstr = str;                return this;            }        }. Accept (innerstring);

  

  

Java Class (ii): 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.