What is a JAVA inner class?

Internal classification

Broadly speaking, internal classes generally include four types: member internal classes, local internal classes, anonymous internal classes and static internal classes.

Member inner class

class Circle {
    
    
    double radius = 0;
     
    public Circle(double radius) {
    
    
        this.radius = radius;
    }
     
    class Draw {
    
         //内部类
        public void drawSahpe() {
    
    
            System.out.println("drawshape");
        }
    }
}

The member inner class can unconditionally locate all member attributes and member methods of the outer class, including privatemembers and static members.

class Circle {
    
    
    private double radius = 0;
    public static int count =1;
    public Circle(double radius) {
    
    
        this.radius = radius;
    }
     
    class Draw {
    
         //内部类
        public void drawSahpe() {
    
    
            System.out.println(radius);  //外部类的private成员
            System.out.println(count);   //外部类的静态成员
        }
    }
}

It is worth noting that when the member variable or method of the inner class and the outer class has the same name, the hidden phenomenon occurs, that is, the members of the inner class of the member are accessed by default. If you need to access a member of the same name of an external class, you need to access it in the following form:

// 外部类.this.成员变量
// 外部类.this.成员方法

If the outer class wants to access the members of the member inner class, it must first create an object of the inner class and access it through a reference to this object.

The member inner class is dependent on the outer class, that is, the premise of creating an object of the inner class is the existence of an object of the outer class.

public class Test {
    
    
    public static void main(String[] args)  {
    
    
        //第一种方式:
        Outter outter = new Outter();
        Outter.Inner inner = outter.new Inner();  //必须通过Outter对象来创建
         
        //第二种方式:
        Outter.Inner inner1 = outter.getInnerInstance();
    }
}
 
class Outter {
    
    
    private Inner inner = null;
    public Outter() {
    
    
         
    }
     
    public Inner getInnerInstance() {
    
    
        if(inner == null)
            inner = new Inner();
        return inner;
    }
      
    class Inner {
    
    
        public Inner() {
    
    
             
        }
    }
}

If the member inner class Inner is decorated with private, it can only be accessed inside the outer class. If it is decorated with public, it can be accessed anywhere; if it is decorated with protected, it can only be in the same package or inherited from the outer class. If it is the default access permission, it can only be accessed under the same package.

Local inner class

A class defined in a method or a scope. The difference with the member inner class is that the access of the local inner class is limited to the method or the scope.

Just like a local variant, there can be no public, protected, private, static modifiers.

class People{
    
    
    public People() {
    
    
         
    }
}
 
class Man{
    
    
    public Man(){
    
    
         
    }
     
    public People getWoman(){
    
    
        class Woman extends People{
    
       //局部内部类
            int age =0;
        }
        return new Woman();
    }
}

Anonymous inner class

That is, an inner class without a name. This class definition disappears immediately and cannot be reused. It is usually used to simplify the code.

The prerequisite for using anonymous inner classes is: must inherit a parent class or implement an interface, and at most inherit one parent class or implement an interface.

Usage rules:

  • 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. Therefore, it is not allowed to define anonymous inner classes as abstract classes.
  • An anonymous inner class cannot define a constructor. Because the anonymous inner class has no class name, the constructor cannot be defined. But an anonymous inner class can define an initialization block, through the instance initialization block to complete what the constructor needs to complete.

Simplified code

abstract class Person {
    
    
    public abstract void eat();
}
 
class Child extends Person {
    
    
    public void eat() {
    
    
        System.out.println("eat something");
    }
}
 
public class Demo {
    
    
    public static void main(String[] args) {
    
    
        Person p = new Child();
        p.eat();
    }
}

In the Childcase where the class in the above code is only used once, writing it as an internal class can simplify the code.

abstract class Person {
    
    
    public abstract void eat();
}
 
public class Demo {
    
    
    public static void main(String[] args) {
    
    
        Person p = new Person() {
    
    
            public void eat() {
    
    
                System.out.println("eat something");
            }
        };
        p.eat();
    }
}

interface

interface Person {
    
    
    public void eat();
}
 
public class Demo {
    
    
    public static void main(String[] args) {
    
    
        Person p = new Person() {
    
    
            public void eat() {
    
    
                System.out.println("eat something");
            }
        };
        p.eat();
    }
}

As long as a class is abstract, the methods in its subclasses can be implemented using anonymous inner classes.

Static inner class

Compared with the member inner class, the static inner class has one more static, that is, it does not need to depend on the outer class. In addition, it cannot use non-static member variables or methods of external classes.

public class Test {
    
    
    public static void main(String[] args)  {
    
    
        Outter.Inner inner = new Outter.Inner();
    }
}
 
class Outter {
    
    
    public Outter() {
    
    
         
    }
     
    static class Inner {
    
    
        public Inner() {
    
    
             
        }
    }
}

Some benefits of inner classes

  1. Each inner class can independently inherit the implementation of an interface, so no matter whether the outer class has inherited the implementation of an interface, it has no effect on the inner class.
  2. It is convenient to organize the classes that have a certain logical relationship together, and can be hidden from the outside world.

Reference

  1. https://zhuanlan.zhihu.com/p/45339875
  2. https://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html
  3. https://www.cnblogs.com/dolphin0520/p/3811445.html

Guess you like

Origin blog.csdn.net/qq_45934120/article/details/107078597