Java Learning Notes (46)-Inner class details

Source: Internet
Author: User

member Inner class
/ * Inner class * Defines classes in another class, called Inner classes inner class * contains classes of inner classes, called external classes outer class * * Applications: Event handling in a form program * Classification: * member Inner class * Local inner class * static Inner class * Anonymous inner class * * member Inner class * 1. Accessing the inner class in an external class allows access to all members of the inner class, including the private decorated * 2. Accessing inner classes outside the outer class, cannot access the members of the private adornment in the inner class * 3. Accessing external classes in internal classes, direct access, If the variable names of the inner and outer classes are the same, use the outer class. this. Variable * / Public classTest12 { Public Static void Main(string[] args) {Outer out=NewOuter ();//Create an object of an external class        /*out.print ();//method of calling External class */          //2. Accessing an inner class outside of a class        //Create an inner class object outside the outer class, syntax: External class. Inner class object name = External class object. New inner Class ();        //outer.inner in=out.new Inner ();//must first create an object of the outer classInnerinch= out.NewInner ();//system.out.println (in.age);//cannot access private members of inner classes        inch. Show (); }}/ * External class Outer, internal class Inner * compiled, generates three bytecode files: Test12.class, Outer.class, Outer$inner.class * Member internal class name: External class name $ internal class name. class */ Class outer{//External class    PrivateString name="Tom";member variables for the//external class    StaticString address="Nanjing"; Class inner{//Inner class        Private intAge= -;//member variable in inner class        PrivateString name="Zhang San"; String address="Beijing"; Public void Show() {String address="Tianjin"; System. out. println ("Show method in inner class");//3. Accessing an external class in an internal classSystem. out. println ("Name in the Inner class:"+ This. name); System. out. println ("Name in the external class:"+outer. This. name); System. out. println ("Age in inner class:"+age); System. out. println ("Address in the Inner class:"+ This. address); System. out. println ("Address in an external class:"+outer.address); System. out. println ("local variable address in inner class:"+address); }    }//1. Accessing inner classes in an external class     Public void Print() {System. out. println ("Print methods in external classes"); Innerinch=NewInner ();//Create an object of the inner class directlySystem. out. println (inch. age);//Access the properties of the inner class to access the private member        inch. Show (); }}
Local inner class
/* Local inner class * is also referred to as the method inner class, that is, the class defined in the method of the outer class * 1. Access scope: Can only be used in methods of external classes, cannot be decorated with modifiers * 2. Non-final-decorated members of methods that cannot access external classes in a local inner class */ Public classTest01 { Public Static void Main(string[] args) {Mouter out=NewMouter (); out. Show (); }}class mouter{String name="Tom"; Public void Show() {FinalintAge= -; System. out. println ("Show method in external class"); Class minner{//local inner classString sex="Male";PrivateString address="Nanjing"; Public void Print() {System. out. println ("Print method in local inner class"); System. out. println ("Member variable in external class name:"+name); System. out. println ("Local variables in external classes age:"+age); }        }//can only be used in methods of external classesMinnerinch=NewMinner (); System. out. println (inch. Sex); System. out. println (inch. address);inch. print (); }}
Static Inner class
/* static inner class * member inner class using static decoration * Cannot access non-static members of Outer class * / Public classTest02 { Public Static void Main(string[] args) {//Create static inner classes without creating objects of external classesSinnerinch=NewSinner ();inch. Show (); }}class souter{StaticString name="Tom";StaticClass sinner{//static inner classString sex="Male"; Public void Show() {System. out. println ("Show method in Static inner class"); System. out. println ("Name in the external class:"+name); }    }}
Anonymous inner class
/ * Anonymous Inner class * is an inner class without a name, because this class only needs to be used once, so there is no need to name * 1. When creating an anonymous inner class, you must declare it as part of the new statement, using: * interface | Abstract class object name = New Interface | Abstract class {* Anonymous inner class *} * 2. When an anonymous inner class is created, an object of an anonymous inner class is automatically created and assigned to the variable */ Public classTest03 { Public Static void Main(string[] args) {Service s=NewService () {//anonymous inner class            //Implement an abstract method in an interface             Public void Show() {System. out. println ("Anonymous inner class for implementing the Service Interface");        }        };        S.show (); Animal pig=NewAnimal () {@Override Public void Play() {System. out. println ("Piggy run .... "); } @Override Public void Show() {System. out. println ("The Pig eats fast ... ");        }        };        Pig.show ();        Pig.play (); Student stu=NewStudent () { Public void Show() {System. out. println ("I am a pupil, Hee hee hehe");        }        };    Stu.show (); }}/ * * Define interface * /Interfaceservice{//Abstract method    voidShow ();}/ * Abstract class * /AbstractClass animal{String name; Public void Show() {System. out. println ("animals are eating .... "); } Public Abstract void Play();} Class student{ Public void Show() {System. out. println ("Hey, hey , hey."); }}

Java Learning Notes (46)-Inner class details

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.