JAVA Note Nine: Internal Classes

An inner class is a class defined inside another class.

Reasons to use inner classes:

  • Inner classes can be hidden from other classes in the same package
  • Inner class methods can access data that defines the scope of the class, including data that would otherwise be private 

 The syntax format is as follows:

class OuterClass {   // 外部类
    // ...
    class NestedClass { // 嵌套类,或称为内部类
        // ...
    }
}

To access the inner class, you can do this by creating an object of the outer class and then creating an object of the inner class. Generally, there are static inner classes and non-static inner classes

Classification of inner classes

1) Local inner class (with class name)
2) Anonymous inner class (without class name)

  • Defined in the member position of the outer class:

1) Member inner class (without static modification)
2) Static inner class (using static modification) 

1. Member inner class (non-static inner class)

A non-static inner class is a class nested within another class. It has access to members of the outer class, often referred to as an inner class.

Creation of inner class object: Because the inner class is nested in the outer class, it is necessary to create the outer class first, and then use the outer class object to create the inner class object.

public class InnerClass {
    public static void main(String args[])
    {
        OutClass myOutClass = new OutClass();
        OutClass.InnerClass myInClass = myOutClass.new InnerClass();
        System.out.println(myOutClass.x+myInClass.y);
    }
}
class OutClass
{
    int x = 10;
    class InnerClass
    {
        int y = 5;
    }
}

First create an external class object, and use the external class object to create an internal class object again. It should be noted that:

        external class name. internal class name internal class object = external class object new internal class constructor

 Internal classes can be modified with private and protected modifiers. If you want external classes not to access internal classes, you can use private modifiers.

2. Static inner class

Static inner classes can be defined using the static keyword, we don’t need to create an outer class to access static inner classes, we can access it directly: (example as above)

public class InnerClass {
    public static void main(String args[])
    {
        OutClass.InnerClass myInClass = new OutClass.InnerClass();
        System.out.println(myInClass.y);
    }
}
class OutClass
{
    int x = 10;
    static class InnerClass
    {
      int y = 5;
    }
}

3. Member inner class and outer class members access each other

An advanced usage of the inner class is to have access to the properties and methods of the outer class:

The example is the same as above, add a string to the external class, we add a method to the internal class (non-static) to get the field x and the string of the external class

public class InnerClass {
    public static void main(String args[])
    {
      OutClass myOutClass = new OutClass();
      OutClass.InnerClass myInnerClass = myOutClass.new InnerClass();
      System.out.println(myInnerClass.getOutClass_Method());
    }
}
class OutClass
{
    int x = 10;
    static String string = "Hello";
    class InnerClass
    {
      int y = 5;
      public int getOutClass_Method()
      {
          return x+string;
      }
    }

}

Notice:

(1) It can be seen from the above examples that the inner class can directly access the characters of the outer class . Inner class access Static inner class cannot access members of outer class. When the outer class field is private, the inner class can still be accessed.

(2) When the members of the external class and the internal class have the same name, the internal class access will follow the principle of proximity by default . If you want to access the members of the external class, you can use (external class name.this.member) to access

class OutClass
{
    private int x = 10;
    class InnerClass
    {
      int x = 5;
      public int getOutClass_Method()
      {
          return this.x;
          //Note that at this time, according to the principle of proximity, the value of x is 5
      }
    }

}

When the return value is return OutClass.this.x, the value of x is 10

(3), external class access member internal class

Because the member internal class can be understood as a member of the external class, so the external access needs to create an object first, that is, use the external class to create an internal class object to access

new OutClass().new InnerClass().getOutClass_Method()

4. Static inner class accesses outer class members

Create outer class object in inner class to access outer class members

Format: new external class constructor.field

class OutClass
{
    private int x = 10;
    static class InnerClass
    {
      int x = 5;
      int y = 100;
      public int getOutClass_Method()
      {
          return new OutClass().x;
      }
    }

}

At this point the value of x is 10. 

In summary:

1. Member inner class

        1. External classes access internal class members, first create external class objects

                Format: external class name. internal class name internal class object = external class object new internal class constructor

                           or

                           new external class name.new internal class name

         2. Internal class accesses external class members

                Format: external class name.this.member (field) external class name.method

                           or

                           direct interview

2. Static inner class

         1. External class accesses internal class members

                   Format: external class name. internal class name internal class object = new external class name. internal class constructor

         2. Internal class accesses external class members

                   Format: new external class name. member (this.field)

 

 

Five, local internal class

1. The local inner class is defined in the local position of the outer class, such as in a method, and has a class name.

2. There cannot be access modifiers when declaring a local class, and the scope of the local class is limited to the block in which the local class is declared.

3. Other external classes cannot access local internal classes

4. The local inner class is defined in the method/code block

6. Anonymous inner class

1. The syntax of the anonymous inner class is rather peculiar. Please pay attention, because the anonymous inner class is not only a class definition, but also an object itself. Therefore, from a grammatical point of view, it has both the characteristics of defining a class and the characteristics of creating an object. ,, so anonymous inner class methods can be called.
2. You can directly access all members of the external class, including private ones.
3. You cannot add access modifiers, because its status is a local variable.
4. Scope: only in the method or code block that defines it.
5. Anonymous inner class—Access---->External class members [Access method: direct access]
6. Other external classes—No access----->Anonymous inner class (because the anonymous inner class status is a local variable)
7. If the members of the external class and the anonymous internal class have the same name, the anonymous internal class will follow the principle of proximity by default. If you want to access the members of the external class, you can use (external class name.this.member) to access

                                                                                                                          

Guess you like

Origin blog.csdn.net/m0_61598337/article/details/128619256