007 - detailed summary of internal knowledge

Keywords: Javascript ECMAScript

Member of class
    attribute
        Static properties
        Non static attribute
    method
        Static method
        Non static method
    constructor
    Code block
         Static code block
         Non static code block
    Inner class: writes one class to the interior of another class

        Member inner class       reference resources   Member variable       Common in source code
        Static member inner class         Static member variable    Common in source code
        Local inner class             local variable
        Anonymous Inner Class             Anonymous object  

Member inner class (I)

Member inner class:
    1. Location
        Class external class name{
            Class internal class name {}
        }
    2. External classes can only be decorated with public default
       The inner class of a member can be modified by four permission modifiers
    3. All resources of the external class can be used directly in the internal class

    4. Internal class resources can be used in external classes (internal class objects need to be created in external methods, and internal class resources can be called in the form of objects.)

         5. Internal classes can be used to achieve the effect of multiple inheritance
    Let the inner class and the outer class inherit from another class

6. Static resources cannot be declared in internal classes, but static constants can be used;

        (because static resources are loaded with the loading of classes, priority should be given to loading. At this time, the class is non-static, and it becomes a contradiction to load static resources first. Therefore, static resources cannot be declared in member internal classes.)

7. The internal class will generate an independent bytecode file naming method
  External class name $internal class name.class
  Outer$Inner.class

public class Outer {
    String name = "Outer";

    class Inner{
        int age = 18;
        public void innerMethod1(){
            System.out.println("this is innerMethod1");
        }
        public void innerMethod(){
            System.out.println(name);
            outerMethod();
        }


    }
    public void outerMethod(){

        System.out.println("this is outerMethod");
    }
    public void outerMethod1(){
        //Create inner class
        Inner inner = new Inner();
        System.out.println("this is outerMethod1");
    }
}

class C{
    public void c(){};
}

class D{
    public void d(){};
}

 

public class Test {
    public static void main(String[] args) {

        Outer outer = new Outer();
        outer.outerMethod1();

        Outer.Inner inner = outer.new Inner();
        inner.innerMethod();
        System.out.println(inner.age);

        Outer.Inner inner1 = new Outer().new Inner();
        System.out.println(inner1.age);
    }

}

Two ways to get inner classes

Outer.Inner inner = outer.new Inner();
inner.innerMethod();
System.out.println(inner.age);

Outer.Inner inner1 = new Outer().new Inner();
System.out.println(inner1.age);

New is a tool. You need to hold new with an external object and use new before you can create an internal class object.

Attribute with the same name in member internal class: (proximity principle)   Now find it in the methods of the inner class, then find the member variables of the inner class, and finally find the member variables of the outer class)

Like name     Outer.this. Property name       In inner class   This. Property name       Attribute name

Static member inner class (II)

Static member inner class:
    1. Location
       Class external class name{
            static class internal class name{
            }
       }
   2. Static internal classes can have static resources (static resources are loaded with the loading of classes, and classes are static, so they can have static resources)
   3. Static internal classes cannot [directly] use non static resources of external classes
       You can create external class objects that use non static resources of external classes
   4. It can achieve the effect of multi inheritance
   5. External classes use static internal class resources
        Static resource internal class name. Resource name
        Non static resource internal class object name. Resource name
   6. The static internal class has an independent bytecode file
      Naming method external class name $internal class name.class
      Outer$Inner.class
   7. Use of internal resources
     7.1 internal static resources
       External class name. Internal class name. Resource name;
     7.2 internal non static resources
      External class name. Internal class name object name=    new external class name. Internal class name ();

        (my understanding)   All static resources need to be called by the class name. The static internal class is the static resource of the external class. Therefore, when instantiating the object, you can use the method of new Outer.Inner()
       Outer.Inner inner = new Outer.Inner();

public class Outer {
    static String name = "Outer";
    int age = 20;
    static int num = 30;
    static class Inner {
        static int num = 20;

        public static void showNum(){
            int num  =10;
            System.out.println(num);
            System.out.println(Inner.num);
            System.out.println(Outer.num);


        }
        public void innerMethod1() {
            Outer outer = new Outer();
            System.out.println("outer.age = " + outer.age);
            System.out.println(name);
        }
        public static void innerStaticMethod1() {
            System.out.println("this is  static innerStaticMethod1 ");
        }
        public void innerMethod2() {
            System.out.println("this is  innerMethod2() ");
        }
    }
    public void outerMethod(){
        //Internal class static resources
        Inner.innerStaticMethod1();
        System.out.println("Inner.num = " + Inner.num);

        Inner inner = new Inner();
        //Internal class non static resource
        inner.innerMethod2();

    }
}
public class Test {
    public static void main(String[] args) {

        Outer outer = new Outer();
        outer.outerMethod();
        System.out.println("----- static state ------");

        System.out.println("Outer.name = " + Outer.name);

        System.out.println("Outer.Inner.num = " + Outer.Inner.num);
        Outer.Inner.innerStaticMethod1();

        System.out.println("----- Non static ------");

       // Outer.Inner inner =  new Outer().new Inner();

        Outer.Inner inner = new Outer.Inner();

        inner.innerMethod2();


    }
}

Local internal class (III)

Local inner class:
    Location: within method
   1. Local internal classes use external resources
     1.1 if a local inner class is used in a common method, all resources of the outer class can be used directly
     1.2 if the local internal class is in the static ordinary method, the external class static resources can be used directly
   2. Local inner classes can only be modified by the permission modifier default   static cannot be used

         You can use final abstract

   3. The local inner class cannot have static resources, but can have static constants

   4. Local internal classes will also generate independent bytecode file naming methods
     External class name $sequence number internal class name.class
     Outer$1A.class
     Outer$2A.class   Serial number starts from 1

   5. If the local variable in the method is used in the local inner class   Then this variable will be modified by final
      Before jdk8   Add final manually
      After jdk8 (included)   Auto add final

public class Outer {
    static String name = "Outer";
    int num = 10;
    public static void main(String[] args) {

    }
    public void method(){
        int n = 30;
          n = 25;
        System.out.println(n);
       /* A a = new A();
        a.innerMethod();*/
       //Existing classes can only be initialized, so the order cannot be changed
        class A{
            final static int m = 10;
            //You can use external class resources
            public void innerMethod(){
                System.out.println(name);
                System.out.println(num);
//                System.out.println(n);
            }
        }

        A a = new A();
        a.innerMethod();
    }
    //What kind of resources do local inner classes want to use
    //The local inner class itself is determined by its method
    //The status of local inner classes is relatively low.
    public static void staticMethod(){
            class A{

                final static int m = 10;
                public void innerMethod(){
                    System.out.println(name);
//                    System.out.println(num);
                }
            }
    }
}

Exercise 1:

public class Test {
    public static void main(String[] args) {
        A a = getValue(); //Returning a class B object is equivalent to A a = new B(), which is equivalent to polymorphism
        int num = a.getNum();//The overridden method is called
        System.out.println("num = " + num);
    }
    public static A getValue(){
        int m = 10;
        class B implements A{
            @Override
            public int getNum() {
                return m;
            }
        }
        B b = new B();
        return b;
    }
}
interface A{
    int getNum();
}

 

 

  Inner name inner class

Anonymous inner class:
    Inner class without name
  1. Create the form of anonymous inner class
    1.1 new class name (constructor) {};
    1.2 new interface name () {};
  2. Creating an anonymous inner class has two functions
     2.1 anonymous subclasses of this class / interface are created
     2.2 the interface / anonymous subclass object is created

  3. The anonymous inner class will generate an independent bytecode file
    Naming method external class name $sequence number.class
            Test$1.class

   4. Anonymous inner class is a special local inner class    In method

(therefore, if the internal class uses the local variable of the method, the local variable will be modified by final by default)

public class Test3 {
    public static void main(String[] args) {
        SuperMan superMan = new SuperMan();
        flying(superMan);
    }
    public static void flying(FlyAble flyAble){ // FlyAble flyAble = new SuperMan()
        flyAble.fly();

    }
}

class SuperMan implements FlyAble{
    @Override
    public void fly() {
        System.out.println("Superman flying.....");
    }
}

 

Posted by Joe_Dean on Sat, 23 Oct 2021 07:46:31 -0700