Java four major internal class-----reproduced

Source: Internet
Author: User

(member) inner class-Common
Features: You can think of an inner class as a "member" of an external class
    • Like members of an external class, an inner class can be decorated with a permission modifier such as private, protected, and so on
    • As with members of an external class, the inner class of the private adornment is visible only to the outer class "public Inner in=new Inner ();", not visible outside the outside class
    • As with members of external classes, private members of external classes (including static members, of course) can also be accessed within the inner class
    • Unlike members of an external class, a static variable cannot be defined in a member's inner class (otherwise it is defined as a static inner class), but a static final variable (that is, a constant) can be defined
    • Unlike members of an external class, the internal class is compiled into a separate class "Outer$inner.class", rather than a domain of the Outer class, when the compilation is complete.
Special use cases: by using an inner class you can indirectly implement multiple inheritance, you can resolve a conflict between multiple interfaces implemented, or a method with the same name between an inherited class and an interface
For example class Robot extends people implement machine {} And if class people and interface machine have a method with the same name, run () at this point, if the above structure is indistinguishable Whether the run () in Robot is inherited or implemented at this time is resolved by having Robot only inherit or implement only, and then defining an inner class in Robot to implement only or inherit only
How the external class accesses the inner class:
    • 1.1, the outer class and its [non-static] methods can directly access the internal classes "new Inner (). Members of the inner class"
    • 1.2. The external class object needs to be created in the [static] method in the outer class before accessing the inner classes "new Outer (). New Inner (). Members of the inner class "
    • 1.3, other local access to the internal class is also "new Outer ()." New Inner (). Members of the inner class "
How the inner class accesses the outer class:
    • 2.1. If the member variables of the outer class are different from the variables of the inner class, the inner class can access the member variables of the outer class directly with the variable name.
    • 2.2. If the same name exists, the inner class can use "outer.this. Variable name" To access an instance variable of an external class (note that it is not outer.inner. Variable name)
    • 2.3, in the internal class directly with the "variable name" or "this. Variable name" Access is the internal class own instance variables
//class other than the external classclass Innerclassdemo {Public static void Main (string[] args) {new Outer (). New Inner (). Innerspeak ();//1.3, other places access to internal classes     }} //External classclass Outer {private static int age = 1;private String name = "External class";private Inner ooo = new Inner ();//1.1, internal classes can be accessed directly in the external classPublic Void Method () {new Inner (). Innerspeak ();//1.1, non-static methods in the outer class can access the inner classes directly    }Public static void Method2 () {new Outer (). New Inner (). Innerspeak ();//1.2, static methods in the outer class access inner classes    } //Inner classclass Inner {private String name = "Ordinary inner class";void Innerspeak () {System.out.println (age++ + "..." + name); //2.1, different names, can access the member variables of the external class directly with the variable name         }void Hello () {String name = "local variable";System.out.println (Outer.this.name + "..." + THIS.name + "..." + name); //2.2, same name, available outer.this. Variable name Access         }    }}
static inner class-more cumbersome
Features: Static internal classes can be treated as a "static member" of an external class
    • Like static members, static inner classes are defined with static and can be modified with public,protected,private
    • As with static members, static inner classes are generated as external classes are loaded, and can be obtained directly with external class class names + static member names
    • Unlike static methods, static internal classes can be defined statically or non-static members can be defined
External classes Access Inner class mode:
    • 1.1. The external class can directly access the static inner class "new Inner (). Member of static inner class"
    • 1.2. The [Static and non-static] members of the external class can access the [static] members of the inner class with the "Outer.Inner. Member"
    • 1.3. The [Static and non-static] members of the external class can use the "New Outer.Inner (). Member" to access [non-static] members of the inner class
    • 1.4, other places access the internal classes are available "new Outer.Inner (). Member", where access to a static member can also be used with "outer.inner. Member"
The inner class accesses the external class way:
    • 2.1, if the [static and non-static] members of the inner class and the [static] members of the external class have different names, they can be accessed directly with the member name.
    • 2.2, when the same name, the [static and non-static] members of the inner class can access the [static] members of the external class with "Outer. Members"
    • 2.3. The [Static and non-static] members of the inner class can access the [non-static] members of the external class with the "New Outer () member".
//class other than the external classClass Innerclassdemo {public static void main (string[] args) {New Outer.inner2 (). Innerspeak ();//1.4 new Outer.inner2 (). INNERSPEAK2 ();//1.4
Outer.Inner2.innerSpeak (); 1.4}} External class Outer {private static int age = 1;   Private String name = "External class"; Private Inner2 ooo = new Inner2 (); 1.1 public void Method () {new Inner2 (). Innerspeak ();//1.1 new Outer.inner2 (). Innerspeak ();//1.1Outer.Inner2.innerSpeak ();//1.2New Outer.inner2 (). INNERSPEAK2 ();//1.3
} Static inner class static class Inner2 {private static String name = "Static Inner class"; static void Innerspeak () {System.out.println (age + "..." + outer.age + "..." + "+ name +" ... "+ new Outer (). Name);//2.1, 2.2, 2.3} void InnerSpeak2 () {System.out.println ("non-static method of static inner class"); }    }}
anonymous inner class-the simplest
Typical usage scenarios:
    • If a class inherits from a class (which is not an abstract class) or implements an interface, then this class is generally required to override or implement some abstract methods.
    • In addition, if this class does not need to add additional members, and we do not need to know the actual type of the class (just to get an object, and no place will be used after the end of the use), then the anonymous inner class is appropriate
Attention:
    • Anonymous inner class is the only "no constructed method class" and cannot be defined in an anonymous inner class
    • Anonymous internal classes are automatically named by the system at compile time Out$1.class
Common format one (single reference): New Fu () {//or interface void Show () {}//subclass "overriding or hiding" method of the parent class}.show (); Common format two (multiple references): Fu f = new Fu () {void Show1 () {} void Show2 () {}};        F.show1 (); F.show2 (); Common format three (parameter pass): Obj.show (New Interfacedemo () {///function parameter is an interface type public void Show1 () {}//the method in the interface usually does not exceed 2 public void Show2 () {}});
local inner classes-infrequently used
Features: a local inner class can be treated as a local variable in an external class method
    • As with local variables, a local inner class is defined in a method (which can, of course, be a static method).
    • As with local variables, local inner classes can only be used in the method in which they are located, and cannot be modified by public, static, etc.
    • As with local variables, only the code after the local inner class is defined to access the local inner class (that is, defined after use)
    • As with local variables, local inner classes can access any member of an external class
    • Unlike local variables, static variables are not defined in local inner classes (regardless of whether the external method is static)
    • Unlike local variables, local inner classes can access only the final decorated local variables (that is, they can only access constants)
External classes do not have access to the external classes in the inner classes of the local inner class:
    • 1.1. If the variable name of the local inner class is different from the member variable of the outer class, the member of the external class can be accessed directly with the variable name.
    • 1.2, if the same name, the local inner class can use "outer.this. Variable name" To access the members of the external class
    • 1.3, in the local inner class can directly use "variable name" or "this. Variable name" To access the internal class own members
Classes other than the Outer class class Innerclassdemo {public static void main (string[] args) {new Outer (). method (3); }}  //  External Class class outer {    private int s = 1;     private static int o = 2;    private  Static int oo = 3;     public void method (final  int k)  {        final int s = 100;         final int oo = 200;         int i = 300;         // Local Interior class         class Inner {             inner (int k)  {                 method ();//I am the method of the external class             &nbsP;    system.out.println (o);//2                 system.out.println (OO);//200                 system.out.println (s);//100                 system.out.println (OUTER.THIS.S) ;//1                // System.out.println (i);//Error             }         }        new inner (k);     }     private void method ()  {         system.out.println ("I am the method of the outer class");     }}

Java four major internal class-----reproduced

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.