java - Inner Class

Keywords: Java

Another class structure is completely embedded in the interior of a class. The embedded class is called the inner class,

Classes nested with other classes are called outer classes. The greatest feature of inner classes is that they can directly access their private properties and reflect the inclusion relationship between classes.

There are a large number of internal classes in the underlying source code.

The five members of the class {attributes, methods, constructors, code blocks, internal classes}

Syntax:

###### classOuLter {/ / external class

​	class Lnner{//Inner class

​	}

}

class Other{//External other classes

}

classification

(1) . defined in a local location of an external class (such as within a method):

1) Local inner class (with class name)

2) Anonymous inner class (no class name)

(2) . defined on the member location of the external class:

1) Member inner class (no static)

2) Static inner class (using static)

1, Local inner class (with class name)

It is defined in the local location of the external class, method body or code block, and has a class name.

1. Local internal classes can directly access external members, including private members.

2. The access modifier cannot be added because its status is a local variable. The modifier cannot be added to a local variable. Final can be added. Adding final means that the class cannot be inherited.

3. The scope is in the method or code block that defines it.

4. When an external class accesses a member of a local internal class, an object must be created within the scope of the internal class.

5. The essence is still a class

public class Outor {
    public static void main(String[] args) {
    Animal animal=new Animal();
    animal.m2();
    }
}
class Sb{//External other classes
    
}
class Animal {
    private int a = 1;

    private void age() {
        System.out.println(a + 100);
    }
    public void m2() {
        /* public class Pig ,error public not allowd here
        1.Access modifier cannot be added. final can be added
        2.A local inner class is defined locally in an outer class, usually in a method
         */
         class Pig {//Local inner class
             //3. Local internal classes can directly access external members, including private members
            public void Prin() {
                System.out.println(a + 99);
                age();
            }

        }
    //4. When an external class accesses a member of a local internal class, an object must be created within the scope of the internal class.
        Pig pig= new Pig();
         pig.Prin();
    }
}

6. Other external classes cannot be accessed, and internal local classes cannot be accessed, because the local internal class status is a local variable.

7. If the members of the external class and the local internal class have the same name, the proximity principle is followed by default. If you want to access the members of the external class, you can use (external class name. this. Member) to access them.

2, Anonymous inner class

(1) The essence is still a class, an internal class, without a name (in fact, there is a name, which can not be seen directly, but is allocated by the system), and it is also an object

Defined in a local location of an external class, such as a method, and has no class name

Syntax:

new class or interface (parameter list){

Class body

};

introduce:

public class animal {
    public static void main(String[] args) {
      Pig pig=new Pig();
      pig.say();
      tiger tiger_=new tiger();
      tiger_.say();
    }
}
interface IG{
    void say();
}
class Pig implements IG{

    @Override
    public void say() {
        System.out.println("pig. . . ");
    }
}
class tiger implements IG{

    @Override
    public void say() {
        System.out.println("tiger. . . ");
    }
}

Because class pig and class tiger are only used once and will not be used later, the traditional method is too cumbersome

To simplify development, anonymous inner classes are used

public class Animal {
    public static void main(String[] args) {
        IG pig=new IG() {//Interface based anonymous inner class
            @Override
            public void say() {
                System.out.println("pig. . . ");
            }
        };
        pig.say();
  		//It can only be used once and cannot be used again. IG tiger=new IG() / / no error will be reported
        //Then call say()//error through tiger
    }
}
interface IG{
    void say();
}

At this time, the compilation type of Pig is IG

The running type of Pig is an anonymous inner class and its name is animal $1 (named by the system)

At the bottom of the system

  IG pig=new IG() {
            @Override
            public void say() {
                System.out.println("pig. . . ");
            }
        };

amount to

class Animal$1 implements IG{

    @Override
    public void say() {
        System.out.println("pig. . . ");
    }

}

IG pig=new IG()

new here

After creating the anonymous inner class Animal , the jdk bottom layer immediately creates an instance of Animal and returns the address to pig

(2) Anonymous inner class is not only the definition of a class, but also an object. Therefore, from the perspective of syntax, it has the characteristics of both defining a class and creating an object

Anonymous inner classes can be called using the following methods

class Pig {
    public  void fi(){
       new A() {
          @Override
          public void f2(){
			System.out.println("Hidden class inner class...");
          }
        }.f2();//Call mode 1
    }
}
class A{
    public void f2(){
        System.out.println("A. . . ");
    }
}
class Pig {
    public  void fi(){
       A a=new A() {
          @Override
          public void f2(){
          System.out.println("Anonymous inner class...");
          }
        };
       a.f2();//Mode II
    }
}
class A{
    public void f2(){
        System.out.println("A. . . ");
    }
}

Anonymous inner class practice

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

        f2(new SB() {
            @Override
            public void f1() {
                System.out.println("tiger tiger. . . ");
            }
        });//This is more concise for enterprises. At this time, the name of the anonymous class is outer $1
    }
        public static void  f2(SB sb){
            sb.f1();

    }
}
interface SB{
    public  void f1();
}

Member inner class

(1) . is defined in the member position of the external class without static modifier

(2) . you can directly access external class members, including private.

(3) . the default access modifier of public private protect can be added arbitrarily, because its status is a member

(4) . the scope is the same as other class members

(5) . member internal class access external class direct access

(6) When the external class accesses the internal class, create the object first

(7) . other external classes access internal classes

Mode 1:

A.A1 a1=A.new A1();

Mode II

Add method public A get(){

return new A();

}//Write A method to return the A object

A.A1 a1=A.get();

(8) . if the external class and internal class members have the same name, follow the go ahead principle

If you want to access external class members, use (external class. this. Member)

public class Outor {
    public static void main(String[] args) {
    A a=new A();
    a.f1();
    }
}
class A{
    private int i=5;
    public void say(){
        System.out.println("tiger tiger. . . ");
    };
    class A1{//Member inner class
        public void hai(){
            System.out.println("pig pig. . . "+i);
        };
    }
    public void f1(){
        A1 a1=new A1();
        a1.hai();
    };
}

Static inner class

(1) . defined in the member position of the external class, with static modifier

(2) . you can directly access all static members of an external class, including private members. You cannot directly access non static members

(3) . the default access modifier of public private protect can be added arbitrarily, because its status is a member

(4) The scope, like class members, is the entire class body

(5) . other external classes access static internal classes

Mode 1

A.A1 a1=new A.A1(); / / access permission is required

Method 2

Write A method to return the A object

public A get(){

return new A();

}//Write A method to return the A object

A.A1 a1=A.get();

(6) . if the external class and internal class members have the same name, follow the go ahead principle

If you want to access external class members, use (external class. Member)

public class Outor {
    public static void main(String[] args) {
    A a=new A();
    a.f1();

    }
}
class A{
    private int i=5;
    static String name="wang";
    public void say(){
        System.out.println("tiger tiger. . . ");
    };
    //Static inner class
    static class A1{
        public void hai(){
            System.out.println("pig pig. . . "+name);
        };
    }
    public void f1(){
        A1 a1=new A1();
        a1.hai();
    };
}

Posted by Waire on Thu, 07 Oct 2021 13:06:15 -0700