[One day a basic series]-java internal class articles | 7-day punch card

[One day a basic series]-java internal class articles | 7-day punch card

Introduction

  • Inner class: In some cases, a class will be defined inside another class. The class defined in other classes is an inner class (also called a nested class), a class that contains an inner class External class
    • Definition format:
    	public class Father {//
       public class Son {//
    
       }
    }
     
    • effect:

      • 1) The inner class provides better encapsulation. The inner class can be hidden in the outer class, and other classes in the same package are not allowed to access the class
      • 2) The inner class members can directly access the private data of the outer class, because the inner class is regarded as its outer class member, and the members of the same class can access each other. But the outer class cannot access the implementation details of the inner class
      • 3) Anonymous inner classes are suitable for creating classes that only need to be used once.
    • The difference between inner class and outer class

      • Grammatically
        • 3.more modifiers can be used for internal analogs than external classes: private, protected, and static. External classes cannot
        • Non-static inner classes cannot have static members
    • Non-static inner class: the member inner class without static modification is a non-static inner class

      • Definition format:
         public class Father {//
         public class NonStaticInnerClass {//
         	//TODO
         }
      }
       
    • Static inner class: the member inner class modified by static is a static inner class

      • Definition format:
         public class Father {//
         public static class StaticInnerClass {//
         	//TODO
         }
      }
       
    • Local inner class: the inner class defined in the method, the class is only valid in the method

      • Define the format
        private void method(){
        	class innerBaseClass{//
        		//TODO
        	} 
        }
        
         

    Note: In actual use, the local inner class is rarely defined because its scope is very small

    • Anonymous inner class
      • Java 8 improvement, suitable for creating classes that only need to be used once
      • Define the format
        new  () |  ( ){
        	//TODO
        }
         
      • rule
        • An anonymous inner class cannot be an abstract class, because when the system creates an anonymous inner class, it will immediately create an object of the anonymous inner class
        • An anonymous inner class cannot define a constructor, because the anonymous inner class has no class name, so the constructor cannot be defined
    • The relationship between non-static inner class objects and outer class objects
      • The non-static inner class object must be parasitic in the outer class object, and the outer class object does not necessarily have a non-static inner class object parasitic in it. Simply put, if there is a non-static inner class object, there must be an outer class object parasitized by it. But when the external class object exists, the non-static internal class object may not be parasitic in the external class object. Therefore, when an external class object accesses a member of a non-static internal class, the non-static common internal class object may not exist at all! When a non-static inner class object accesses a member of an outer class, the outer class object must exist
    • The relationship and difference between lambda expressions and anonymous inner classes
      • Connection: Lambda expression is a simplification of anonymous inner class, so it can partially replace the role of anonymous inner class
        • Lambda expressions, like anonymous inner classes, can directly access "effectively final" local variables and member variables of outer classes (including instance variables and class variables)
        • The objects created by Lambda expression expressions are the same as those generated by anonymous classes, and both can directly call the default methods inherited from the interface
      • the difference:
        • An anonymous inner class can create instances for any interface no matter how many abstract methods the interface contains, as long as the anonymous inner class implements all abstract methods; but Lambda expressions can only create instances for functional interfaces

        • Anonymous inner classes can create instances for abstract classes or even ordinary classes; but Lambda expressions can only create instances for functional interfaces

        • The method body of the abstract method implemented by the anonymous inner class is allowed to call the default method defined in the interface; but the code block of the Lambda expression is not allowed to call the default method defined in the interface

  • Summary: Regardless of whether the internal class or the external class, the principle is related to the usage scenario, so don't be too entangled in how to define the corresponding class, the result can be oriented.