1, the definition of the inner class:
An inner class can be defined in another class and can be defined in a function or even as part of an expression.
2, the classification of the Inner class:
The inner classes in Java are divided into four types:
member Inner class member inner class
Local internal class local inner class
Anonymous inner class anonymous inner class
Static inner class static inner class
2.1, Member internal class member inner class
The member inner class is the most common inner class, defined as the interior of another class
PackageInner; Public classOuter {//External ClassString Outerlabel = "outer"; Private inti = 1; Static intj = 11; Public voidPrintouterlabel () {System.out.println (Outerlabel+ "\ t" +i+ "\ T" +j); } classinner{//Inner classString Innerlabel = "inner"; Public voidPrintinnerlabel () {System.out.println (Outerlabel+ "\ t" +innerlabel+ "\ T" +i+ "\ T" +j); } } //an external class invokes a reference to an inner class and needs to create a new object with the New keyword Public voidPrintinner () {Inner Inner=NewInner (); Inner.printinnerlabel (); } Public Static voidMain (string[] args) {Outer Outer=NewOuter (); Outer.printouterlabel (); Outer.printinner (); }}
Output
Outer 1 outer inner1 11
Description
1. An inner class can unconditionally access all member properties and member methods of an external class, including private and static members.
2. An object of an inner class is created only if it is associated with an object of its outer class
3. When an inner class has a member variable or method with the same name as an external class, a hidden phenomenon occurs, that is, by default, members of the inner class are accessed
When an inner class needs to access a variable or method with the same name as the outer class, the following method
The external class. This . Member variable external class. this. Member method
PackageInner; Public classOuter {//External ClassString Label = "outer"; Private inti = 1; Static intj = 11; Public voidPrintlabel () {System.out.println (Label+ "\ t" +i+ "\ T" +j); } classinner{//Inner classString Label = "inner"; Public voidPrintlabel () {//calling an external class in an inner class with the same name variable, methodSystem.out.println (Outer. This. label+ "\ t" +label+ "\ T" +i+ "\ T" +j); Outer. This. Printlabel (); } } //an external class invokes a reference to an inner class and needs to create a new object with the New keyword Public voidPrintinner () {Inner Inner=NewInner (); Inner.printlabel (); //methods for inner classes } Public Static voidMain (string[] args) {Outer Outer=NewOuter (); Outer.printlabel (); Outer.printinner (); }}
Output
Outer 1 outer inner 1 outer 1 11
In the inner class you need to generate a reference to an external class object, the following methods
The external class. This
In this case, the inner class gets a reference to the outer class, so you can manipulate the outer class
PackageInner; Public classOuter {//External ClassString Label = "outer"; Private inti = 1; Static intj = 11; Public voidPrintlabel () {System.out.println (Label+ "\ t" +i+ "\ T" +j); } classinner{//Inner class PublicOuter Outer () {returnOuter. This; } } //External class returns a reference to an inner class PublicInner Inner () {return NewInner (); } Public Static voidMain (string[] args) {Outer Outer=NewOuter (); Outer.Inner Inner=Outer.Inner (); Inner.outer (). Printlabel (); }}
When other objects want to create an object of the inner class, they need to pass
New External class (). New inner class ()
PackageInner; Public classOuter {//External ClassString Label = "outer"; Private inti = 1; Static intj = 11; Public voidPrintlabel () {System.out.println (Label+ "\ t" +i+ "\ T" +j); } classinner{//Inner class PublicOuter Outer () {returnOuter. This; } } //External class returns a reference to an inner class PublicInner Inner () {return NewInner (); } Public Static voidMain (string[] args) {Outer Outer=NewOuter (); Outer.Inner Inner= Outer.NewInner (); Inner.outer (). Printlabel (); }}
The output is the same as above
The inner class is accessed through. New, and the inner class is the object that returns the outer class, through which we can manipulate the objects, variables, etc. of the outer class.
2.2, local internal class local inner class
A complete class created within the scope of a method, called a local inner class
PackageInner; Public classParcel2 {//External Class PublicDestination Destination (String s) {//method within classPdestinationImplementsdestination{//inner class within method and local inner class PrivateString label; Privatepdestination (String to) { This. Label =to ; } PublicString Readlabel () {returnlabel; } } return NewPdestination (s); } Public Static voidMain (string[] args) {Parcel2 P=NewParcel2 (); Destination D= p.destination ("label"); System.out.println (D.readlabel ()); }}
Interface
Package Inner; Public Interface Destination { String readlabel ();}
Output
Label
A local inner class is like a local variable inside a method, and it cannot have public, protected, private, or static modifiers.
A local inner class belongs to its method, not to an external class
2.3. Anonymous inner class anonymous inner class
Interface
Package Inner; Public Interface Contents { int value ();}
PackageInner; Public classParcel7 { PublicContents Contents () {//The contents method combines the generation of the return value with the definition of the class that represents the return value. return NewContents () {Private inti = 10; Public intvalue () {returni; } }; } Public Static voidMain (string[] args) {PARCEL7 P=NewParcel7 (); Contents C=p.contents (); System.out.println (C.value ()); }}
The key to establishing an anonymous inner class is to override one or more methods of the parent class.
The above anonymous inner class is equivalent to:
PackageInner; Public classparcel7b {classMycontentsImplementscontents{Private inti = 10; Public intvalue () {returni; } } PublicContents Contents () {return Newmycontents (); } Public Static voidMain (string[] args) {parcel7b P=Newparcel7b (); Contents C=p.contents (); System.out.println (C.value ()); }}
The above is the member's inner class
2.4, static internal class static inner class
When the inner class is static, it is the static inner class
Attention:
(1) To create an object of a static inner class, the object of its outer class is not required
(2) Non-static external class object cannot be accessed from an object in an inner class
PackageInner; Public classOuter {//External Class StaticString Label = "outer"; intSize = 10; Static classinner{//static modifier Public voidprint () {System.out.println (Label); } } Public Static voidMain (string[] args) {Outer.Inner Inner=NewOuter.Inner (); Inner.print (); }}
3. Why do I need internal classes?
An inner class inherits from a class or implements an interface, and the inner class's code operation creates an object of the outer class, so it can be assumed that the inner class provides some kind of window into the outer class
Each inner class can be independent of the implementation of an interface, regardless of whether the external class has inherited an implementation of an interface, there is no effect on the inner class
Inner classes allow inheriting multiple abstract classes
Reference: Introduction to Algorithms---write poorly on your own
Java: Inner class