Java inner class? Look, that's enough

Suunsr 2022-05-22 12:26:26 阅读数:245

javainnerclasslook

Catalog

One . The definition of an inner class

One . Member inner class

1: Definition

2. How to use the inner class / The rules :

3. Creation of class objects inside members :

​4. matters needing attention :

Two . Static inner class

1. Definition :

2. Usage method / The rules :

3. Creation of static internal class objects

4. Add

  3、 ... and . Method inner class

1. Definition

  Four . Anonymous inner class


One . The definition of an inner class

The so-called inner class , Is to set the definition of class structure inside another class . For example, in real life

The relationship between automobile engine and automobile , The engine is set inside the automobile , Or the relationship between human heart and human body , The heart is inside the human body . Inner classes also belong to a kind of encapsulation , Play a protective role .

One . Member inner class

1: Definition

Member inner classes can be compared to member methods , Is defined directly in the class , Without any modifiers (static) Defined classes , It's a member inner class

2. How to use the inner class / The rules :

a. The inner class of a member must depend on the existence of the outer class , You must an object of an existing external class to produce an internal class of members .

b. An inner class is a relatively independent entity , Not with external classes is-a The relationship between

c. The inner class can directly access the elements and methods of the outer class ( Include private domain ), The external class must access the elements and methods of the internal class through the objects of the internal class ( Include private domain ).

3. Creation of class objects inside members :

1. Internal creation of external classes

Inner class name Internal class reference = new Inner class ();

 2. Create an inner class object outside the outer class ( Internal classes need to be visible to external classes )

4. matters needing attention :

1. Internal and external classes can easily access each other's private domains , The inner class can directly access the private domain of the outer class ( An object of an external class is hidden in the inner class ), The outer class needs to access the private domain of the inner class , Must be accessed through an object .

2. For member inner classes , Static variables cannot be defined , For external classes , Member inner classes cannot be used in static methods of outer classes .

Two . Static inner class

1. Definition :

Static inner classes can be compared to static variables , It is defined in a class , Use static The decorated inner class is the static inner class .

2. Usage method / The rules :

1. Static inner classes do not need to rely on external class objects .

2. A static inner class is just an ordinary class , It's just inside a class .

3. Creation of static internal class objects

The inner part of the outer class

The outer part of the outer class

4. Add

1. A static inner class is just an ordinary class , It's just inside a class .

2. Static inner classes can define member variables , Cannot access member variables of external classes

  3、 ... and . Method inner class

1. Definition

Analogy to local variables , Classes defined directly inside methods , No access modifiers are allowed , Completely hidden from the outside ( Out of this way , This class is gone ) 

1. Method inner class cannot define static Domain , In addition, the usage is basically the same as that of the inner class of the member .

2. If the formal parameters of the method are used in the internal class of the method , The parameter is implicit final Statement

  Four . Anonymous inner class

Anonymous inner classes are special versions of method inner classes , Do not write the class name directly . Comply with all the requirements of the inner class of the method , Anonymous internals inherit a class or implement an interface by default .

The previous interface parameter transfer uses :

Use anonymous inner class :

 

 

 

 

copyright:author[Suunsr],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/142/202205211828001124.html