Brief analysis of internal classes

                                                    Inner class

Internal class definition: In Java, it is allowed to define a class inside a class. Such a class is called an inner class and is also called a nested class. The class in which this inner class is called an outer class.

Category: static inner class method inner class member inner class anonymous inner class

Points to note when creating objects of inner classes:

First: objects can be created directly inside the external class.

Method: inner s = new inner ();

   Second: You cannot directly create objects outside the external class, you must use the class name of the external class to determine.

Method: Suppose A is an external class and B is an internal class

A.B b=newA().new B();

   Some features of inner classes:

First: The inner class is still an independent class. After compilation, the inner class will be compiled into an independent .class file, but the class name of the outer class and the $ symbol are in front.

Second: Inner classes cannot be accessed in ordinary ways. The inner class is a member of the outer class, so the inner class can freely access the member variables of the outer class, whether or not it is private.

Third: Members of the inner class can directly access the members of the outer class (including private members).

Fourth: better encapsulation than the category.

The focus is on anonymous inner classes.

   The anonymous inner class must follow new, and use it to implicitly implement an interface or a class without a class name. According to polymorphism, we use its parent class name. Because anonymous inner classes belong to local classes, all restrictions on local classes take effect. An anonymous inner class is the only class without a constructor. When an anonymous inner class is compiled, the system automatically names Out $ 1.class. If the type of an object at compile time is an interface, then the type it runs is the class that implements the interface. (It involves polymorphism, turn-up, combination, inheritance, and certain organizational methods)

   Features of anonymous inner classes:

   First: the only class without a constructor.

   Second: No permission settings such as privatepublic protected static

   Third: Only instances of anonymous inner classes can be instantiated.

   Fourth: You cannot define classes without static methods.

Advantages of inner classes:

The code is neat, compact, and streamlined. You can directly use the interface or class, so you don't need to add other classes or interfaces, just switch to using the existing class or interface.

Disadvantages: poor readability.

           Instantiation

Code demo:

packagecom.yc.bean;

 

public class Outer {

   private int num = 4; // Define member variables

 

   public void test(){

      Inner inner = newInner();

      inner.show();

   }

 

   // The following code defines a member internal class

   class Inner {

      void show(){

        // Access the member variables of the outer class in the method of the inner class of the member

        System. Out .println ( " Access to private members of external classes \ t" + "num =" + ++ num );

      }

   }

}

packagecom.yc.bean;

 

public class Example{

   public static voidmain(String[] args){

      Outer.Inner inner = new Outer (). New Inner (); // Create inner class object

      inner .show (); // Call test () method

   }

  

}

            Test results show

Access to private members of external classes num = 5

packagecom.yc.bean;

 

// The inner class is equivalent to the attribute of the outer class. The generation of the inner class object depends on the outer class object. As long as the attribute can have modifiers, it can modify the inner class. 

// Internal classes will also generate class files during compilation . Named as: outer class name $ inner class name. Class

public class Myclass{

   public Myclass(){

      System. Out .println ( " External class ............" );

   }

 

   class M {

      public M() {

        System. Out .println ( " Inner class ........." );

      }

   }

 

   public static voidmain(String[] args) {

 

      Myclass my = new Myclass (); // External class declaration

      Myclass.M m = my . New M (); // Inner class declaration

   }

}

           Test results show

        Outer class ...

Inner class ...

 

 

                   

 

 

Published 24 original articles · praised 36 · 20,000+ views

Guess you like

Origin blog.csdn.net/tanjunchen/article/details/51429189