preface

During the development of the project, we need to use entities to receive the parameters passed. If the Json structure from the front end is complex, we need to use relatively complex entities to receive the parameters, i.e. one DTO reference another DTO. At this time, if we build two entities, it still looks not “concise”. So that’s where the inner class comes in. Put internal complex structures into inner classes. The static interior class, the highlight of this paper, comes on stage.

Gleanings – What is an inner class

In Java, a class can be defined inside another class or a method. Such classes are called inner classes. Inner classes in the broad sense generally include these four types: member inner classes, local inner classes, anonymous inner classes, and static inner classes. Here’s a look at the use of these four inner classes.

1. Member inner classes

A member inner class is the most common inner class. It is defined as being inside another class and has the following form:

class Circle { double radius = 0; public Circle(double radius) { this.radius = radius; } class Draw {// inner class public void drawSahpe() {system.out.println (" drawShape "); }}}Copy the code

In this way, the class Draw looks like a member of the Circle class, which is called the outer class. Member inner classes have unconditional access to all member attributes and member methods of the external class (both private and static).

class Circle { private double radius = 0; public static int count =1; public Circle(double radius) { this.radius = radius; } class Draw {// inner class public void drawSahpe() {system.out.println (radius); // The private member of the external class system.out.println (count); // Static member of the external class}}}Copy the code

Note, however, that when a member inner class has a member variable or method with the same name as the outer class, hiding occurs, meaning that members of the inner class are accessed by default. If you want to access a member of an external class with the same name, you need to access it in the following form:

While an inner class can access a member of an outer class unconditionally, an outer class is not so free to access a member of an inner class. To access a member of an inner class in an outer class, you must first create an object of the inner class and then access it via a reference to the object:

class Circle { private double radius = 0; public Circle(double radius) { this.radius = radius; getDrawInstance().drawSahpe(); } private Draw getDrawInstance() {return new Draw(); } class Draw {// inner class public void drawSahpe() {system.out.println (radius); // Private member of external class}}}Copy the code

A member inner class exists in dependency on an external class, that is, if an object of the member inner class is to be created, an object of the external class must exist. The general way to create a member inner class object is as follows:

Public class Test {public static void main(String[] args) {public static void main(String[] args) {// Outter.Inner inner = outter.new Inner(); 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() { } } }Copy the code

Inner classes can have private access, protected access, public access, and package access. In the example above, if the Inner class of a member is decorated with private, it can only be accessed inside the outer class. If the Inner class is decorated with public, it can be accessed anywhere. If protected, it can only be accessed under the same package or if it inherits from an external class. If it is the default access permission, it can only be accessed under the same package. This is a bit different from external classes, which can only be qualified with public and package access permissions. My personal understanding is that because a member inner class looks like a member of an outer class, it can have multiple permission modifiers just like a member of a class.

2. Local inner classes

A local inner class is a class defined in a method or scope. It differs from a member inner class in that access to a local inner class is limited to the method or scope.

class People{ public People() { } } class Man{ public Man(){ } public People getWoman(){ class Woman extends People{ Int age =0; } return new Woman(); }}Copy the code

Note: A local inner class, like a local variable inside a method, cannot have public, protected, private, or static modifiers.

Anonymous inner classes

Anonymous inner classes are probably the ones we use the most when writing code. Using anonymous inner classes when writing code that listens for events is not only convenient, but also makes the code easier to maintain. The following code is an Android event listener:

scan_bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method  stub } }); history_bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } });Copy the code

This code sets up listeners for the two buttons, where anonymous inner classes are used. In this code:

new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         
    }
}
Copy the code

This is the use of anonymous inner classes. The code needs to set up a listener object for the button. Using an anonymous inner class can generate a corresponding object while implementing methods in the parent class or interface, but only if the parent class or interface exists first. Of course, the following is also possible, as above using anonymous inner class to achieve the same effect.

private void setListener() { scan_bt.setOnClickListener(new Listener1()); history_bt.setOnClickListener(new Listener2()); } class Listener1 implements View.OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method  stub } } class Listener2 implements View.OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub }Copy the code

This approach achieves the same effect, but is verbose and difficult to maintain, so you typically use the anonymous inner class approach to write event listener code. Similarly, anonymous inner classes cannot have access and static modifiers.

The anonymous inner class is the only class that does not have a constructor. Because it has no constructor, the use of anonymous inner classes is limited, and most of them are used for interface callbacks. Anonymous inner classes are automatically named Outter$1.class by the system at compile time. In general, anonymous inner classes are used to inherit from other classes or implement interfaces. There is no need to add additional methods, just implementations or overrides of inherited methods.

Static inner classes

A static inner class is a class defined in another class with the keyword static in front of it. A static inner class does not depend on an external class, just like a static member property of a class, and it cannot use non-static member variables or methods of an external class. This makes sense because you can create an object of a static inner class without an object of an external class. Allowing access to non-static members of an external class creates a contradiction because non-static members of an external class must be attached to a concrete object.

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

Craftsman – Skillfully accepts parameters with inner classes

To solve the problem of parsing complex interface parameters, we use inner classes to accept. You can refer to the following demo

Json parameter format

{" name ", "beauty", "age" : 18, "school" : {" schoolName ":" the north ", "grade" : 2}}Copy the code

The receiving entity

@Data public class User { private String name; private int age; public static class schoolInfo{ private String schoolName; private int grade; }}Copy the code

We just need to define the DTO format as Json.

summary

Software development is like building a house. We have to constantly think about how to design a house to make it more beautiful. More comfortable. The code is clean and comfortable to look at and easy to maintain.