JavaInner class in (member inner class, Static inner class, local inner class, anonymous inner Class)
Myth 丿 The Little Prince's blog home
Let's look at this passage first: People are made up of body results, such as the brain, limbs and organs. And the heart that makes up our human body it also has its own properties and behavior (blood, beating), obviously, there is no one-sided use of attributes or methods to express a heart, but the need for a class, but this class depends on the existence of human beings (because the heart can not be separated from the human body to survive, of course, not considering the modern Only under normal circumstances), the heart is written in the human interior. The inner class is equivalent to one of the organs.
First look at what the inner class is: Define another class inside the class. Yes, the definition is so simple. If you define a class Innerinside a class Outer , Inner is called an inner class, and Outer is called an external class. The inner class is defined in the following format:
Public class name of external class { // member of external class public class Internal class name { // member of inner class }}
What are the benefits of using internal classes?
1) Achieve multiple inheritance ;
2) Inner classes can be very well hidden: the general non-internal class, is not allowed to have private and protected permissions, but the inner class can be
3) reduced the size of the bytecode file generated after the class file is compiled
Disadvantages of using inner classes: Make the program structure unclear.
The inner class will also produce a . class file after compilation, but the file name is: External class name $ internal class name . Class
Types of inner classes: member inner class, Static inner class, local inner class, anonymous inner class. The following is a detailed study of the four kinds of internal classes of specific use.
First, members of the internal class
1, the member inner class is also called the instance inner class. Application: Each external class object requires an instance of an inner class, and the inner class is inseparable from the existence of an external class (the equivalent of a heart to the human body)
2. Characteristics of the Members ' inner classes:
exists as a member of an external class, parallel to the properties, methods of the outer class
member Inner class holds references to external classes
static variables and methods cannot be defined in a member's inner class
3, use the format:
New= outer. New Inner ();
Code Demo One:
/** Member Inner class*/ Public classBody {String arm; String leg; String Blood; PublicBody (string arm, string leg, String blood) {Super(); This. Arm =arm; This. Leg =leg; This. Blood =Blood; } //Inner class Heart classheart{String name; voidWork () {System.out.println ("The Heart is Giving" +arm+leg+ "+blood); } };//Note that it is possible to add no semicolons here, and there is no problem with compiling}
Public class Test { publicstaticvoid main (string[] args) { body body= New Body ("Two Arms", "two Legs", "blood"); Body.heart Heart=body. New Heart (); Heart.work (); }}
Run:
Code Demo Two:
/** Member Inner class*/ Public classOuter {PrivateString name= "Hello World"; Public classinner{ Public voidprint () {System.out.println (name); } }; Public voidPrint () {//defining an external class method NewInner (). print ();//invoking a method by instantiating an object from an inner class } Public Static voidMain (string[] args) {Outer Outer=NewOuter (); Outer.print ();//methods for calling external classes }}
Run:
Explain, in the above code,new Inner (). print (); Code equivalent to Inner Inner=new Inner (); Inner.print ();
Second, static internal class
1.if the inner class uses a static declaration, this inner class is called a static inner class. (In fact, it is also equivalent to an external class) through an external class . Internal classes to access.
2, static internal class use occasions: The inner class does not require an instance of an external class (note the distinguished member inner Class), the static inner class exists only for the external class to provide services or logically belong to the external class, and logically can exist alone.
3. Characteristics of static inner class:
Static inner classes do not hold references to external classes
Static inner classes can access external static variables if the member variables that access the external class must be accessed through an instance of the external class
4. only internal classes in Java can be static
Use format:outer.inner Inner = new Outer.Inner ();
Code Demo One:
/** Static Internal*/ Public classCompany {String Companynam; StaticString Country; Static classclear{String name; PublicClear () {//TODO auto-generated Constructor stub } PublicClear (String name) {Super(); This. Name =name; } Public voidWork (string name) {string Na=NewCompany (). companynam= "Lenovo"; Country= "China"; SYSTEM.OUT.PRINTLN (Name+ "for" +na+ "cleaning, the company belongs to" +country); } }}
Public class Test { publicstaticvoid main (string[] args) { company.clear ZCL =new company.clear (); Zcl.work ("Shen_hua");} }
Run:
Code Demo Two:
/** Static inner class*/ Public classOuter {Private StaticString info= "Hello World"; Private intI=8; Static classinner{intnum=NewOuter (). I;//gets the non-static member variable of the outer class, and the sky member variable can be used directly Public voidShow () {System.out.println (Info+"---"+num); } };}
Public class Test { publicstaticvoid main (string[] args) { Outer.Inner Inner=new Outer.Inner (); Inner.show (); }}
Run:
Third, the local inner class:
1, the local inner class is also called the area inline class, the local inner class is similar to the member inner class, however, the zone inline class is the embedded class defined in a method.
2, use the occasion: if the inner class object is used only for a method of the outer class, use the local inner class
3. Characteristics:
Within the method, the scope is limited to the method
Hold external class object references as appropriate
Cannot use private,protected, public modifier
Cannot contain static members
Code Demo One:
/** Local inner class*/ Public classSchool {String schoolname; String Buss= "Nurturing Talents"; intStudentnum; PublicSchool () {//TODO auto-generated Constructor stub } PublicSchool (String schoolname, String buss,intstudentnum) { Super(); This. Schoolname =Schoolname; This. Buss =Buss; This. Studentnum =Studentnum; } //Propaganda Public voidShow () {Final Doubletvmoney=10000; Final Doublenetmoney=20000; classadverteam{String teamname= "Shen_hua"; //TV Propaganda Public voidtvShow () {System.out.println ("Propaganda is:" +teamname); System.out.println ("This is TV propaganda, school name" +schoolname+ ", Business content:" +buss+ ", Number of schools:" +studentnum+ ", promotional expenses" +Tvmoney); } //Network Propaganda Public voidnetShow () {System.out.println ("Propaganda is:" +teamname); System.out.println ("This is the network propaganda, the school name" +schoolname+ ", the Business content:" +buss+ ", the school Number:" +studentnum+ ", the propaganda expense" +Netmoney); } } NewAdverteam (). TvShow (); NewAdverteam (). NetShow (); }}
Public class Test { publicstaticvoid main (string[] args) { School Qinghua= New School ("Tsinghua", "Internet training", +); Qinghua.show (); }}
Run:
Iv. Anonymous Internal classes
1. If an inner class is used only once throughout the operation, it can be defined as an anonymous inner class. The anonymous inner class, which is an inner class without a name, is A mechanism that Java has designed to facilitate our programming, because sometimes internal classes just need to create an object of its own, and no longer use this class. It is more appropriate to use anonymous inner classes at this time.
2, use occasions: simplify the use of internal classes
3. Characteristics:
Created with new , no specific location
Anonymous class created, default inheritance or type after implementation of new
Determines whether external class object references are held depending on usage
The . Class file that is generated after the embedded anonymous class is compiled is named "External class name $ number . Class", numbered 1 , 2 , 3 ... .. N , which is numbered x the file corresponds to the first x an anonymous class
Code Demo:
/* * anonymous inner class public interface person { public void run ();}
/**publicclass Test {publicstatic void Main (string[] args) {person p=new person () { public void run () { System.out.println ("Anonymous inner class implementation");} }; P.run (); }}
Run:
Inner classes in Java (Member inner class, Static inner class, local inner class, anonymous inner Class)