Java knowledge combing 03 | detailed explanation of internal classes

Keywords: Java Back-end IDEA

1, Definition of inner class

  • You can put the definition of one class inside the definition of another class, which is the inner class.

2, Type of inner class

  • In Java, inner classes are mainly divided into member inner classes, local inner classes, anonymous inner classes and static inner classes.

1. Member internal class

  • Member internal class definition format.
class A{
    class B{

    }
}
  • The inner class of a member unconditionally accesses the properties and methods of the outer class.
public class Outer {
    private String name = "Outer";
    public void run(){
        System.out.println("Outer run");
    }

    class Inner{
        public void say(){
            System.out.println(name);
            run();
        }
    }

}
  • When an external class wants to access the properties or methods of an internal class, it must create an internal class object, and then access the properties or methods of the internal class through the object.

  • The Inner class is defined inside the Outer class, which is equivalent to the position of the member variable of the Outer class. The Inner class can use any access modifier.

  • Static members cannot be defined in the internal class of members, but static fields can exist, provided that they need to be decorated with the final keyword.

public class Outer {
    private static String oName = "name = Outer";
    public void run(){
        System.out.println("Outer run");
    }

    public static void main(String[] args) {
        Outer outer=new Outer();
        Outer.Inner inner=outer.new Inner();
        System.out.println(inner.iName);
        inner.say();
    }

    class Inner{
        private String iName="name = Inner";
        public static final int age=18;
        public void say(){
            System.out.println(oName);
            System.out.println("age = "+age);
            run();
        }
    }
}
//Output results:
name = Inner
name = Outer
Outer run
  • If the attribute or method of the member's internal class has the same name as the external class, these attributes and methods of the external class will be hidden in the internal class. You can call the external class. This. Attribute / method according to this format.
public class Outer {
    private static String name = "name = Outer";
    public void run(){
        System.out.println("Outer run");
    }

    public static void main(String[] args) {
        Outer outer=new Outer();
        Outer.Inner inner=outer.new Inner();
        inner.show();
    }

    class Inner{
        private String name="name = Inner";
        public void run(){
            System.out.println("Inner run");
        }
        public void show(){
            System.out.println(new Inner().name);
            System.out.println(name);
            run();
            System.out.println(new Outer().name);
            System.out.println(Outer.this.name);
            Outer.this.run();
        }
    }

}
//Output results:
name = Inner
name = Inner
Inner run
name = Outer
name = Outer
Outer run

2. Local internal class

  • Local internal class definition format
class A{
    public void show(){
        class B{
            
        }
    }
}
  • The local inner class unconditionally accesses the properties and methods of the outer class.
public class Person {

    private String name="Zhang San";
    public void introduce(){
        System.out.println("He is"+name);
    }

    public void show(){
        class Hobby{
            private String hobby="dance";
            public void say(){
                introduce();
                System.out.println(name+"love"+hobby);
            }
        }
    }
}
  • The local internal class is nested in the method and scope, but its scope has changed. It can only be used in the method and attribute, and the method and attribute will become invalid.
  • Static members cannot be defined in local internal classes, but static fields can exist, provided that they need to be decorated with the final keyword.
public class Person {

    private String name="Zhang San";
    public void introduce(){
        System.out.println("He is"+name);
    }

    public void show(){
        class Hobby{
            public static final String intr="introduce:";
            private String hobby="dance";
            public void say(){
                System.out.println(intr);
                introduce();
                System.out.println(name+"love"+hobby);
            }
        }
        Hobby hobby=new Hobby();
        hobby.say();
    }

    public static void main(String[] args) {
        Person person=new Person();
        person.show();
//      Hobby hobby=new Hobby();  Compilation error
//      Person.Hobby hobby=new Person.new Hobby();  Compilation error
    }
}
//Output results:
introduce:
He is Zhang San
 Zhang Sanai dances
  • If the attribute or method of the local internal class has the same name as the external class, these attributes and methods of the external class will be hidden in the internal class. You can call the external class. This. Attribute / method according to this format.
public class Person {
    private String name="Person";
    public void say(){
        System.out.println(name);
    }
    public void show(){
        class Hobby{
            private String name="Hobby";
            public void say(){
                System.out.println(name);
            }
        }
        Hobby hobby=new Hobby();
        System.out.println(hobby.name);
        hobby.say();
        System.out.println(Person.this.name);
        Person.this.say();
    }
    public static void main(String[] args) {
        Person person=new Person();
        person.show();
    }
}
//Output results:
Hobby
Hobby
Person
Person

3. Anonymous inner class

  • Anonymous inner classes are inner classes without names. Because there is no name, anonymous inner classes can only be used once. It is usually used to simplify code writing. But there is another prerequisite for using anonymous inner classes: you must inherit a parent class or implement an interface.
//Anonymous inner classes are not used to implement abstract methods
abstract class Person {
    public abstract void say();
}

class Student extends Person {
    public void say() {
        System.out.println("say");
    }
}

public class Test {
    public static void main(String[] args) {
        Person p = new Student();
        p.say();
    }
}
//Output results:
say
  • Use anonymous inner classes to implement abstract methods and interfaces.
//Abstract method
abstract class Animal {
    public abstract void shout();
}

public class Test{
    public static void main(String[] args) {
        Animal animal=new Animal(){
            public void shout() {
                System.out.println("shout");
            }
        };
        animal.shout();
    }
}
//Output results:
shout
    
//Interface
interface Animal {
    void shout();
}

public class Test{
    public static void main(String[] args) {
        //This code can replace the following code: Animal Animal = () - > system.out.println ("shot");
        Animal animal=new Animal(){
            public void shout() {
                System.out.println("shout");
            }
        };
        animal.shout();
    }
}
//Output results:
shout
  • Anonymous inner classes are most commonly used in the implementation of multithreading, because to implement multithreading, you must inherit the Thread class or the Runnable interface.
//Anonymous inner class implementation of Thread class
public class Test {
    public static void main(String[] args) {
        Thread t = new Thread() {
            public void run() {
                for (int i = 1; i <= 5; i++) {
                    System.out.print(i + " ");
                }
            }
        };
        t.start();
    }
}
//Output results
1 2 3 4 5
    
//Anonymous inner class implementation of Runnable interface
public class Test {
    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                for (int i = 1; i <= 5; i++) {
                    System.out.print(i + " ");
                }
            }
        };
        Thread t = new Thread(r);
        t.start();
    }
}
//Output results
1 2 3 4 5

4. Static internal class

  • Static internal class definition format
class U {
    static class I {
        
    }
}
  • Static can not only modify member variables, methods and code blocks, but also modify internal classes. The internal classes modified by static are called static internal classes.

  • Static inner classes can only access static member variables and methods of peripheral classes, but cannot access non static member variables and methods of peripheral classes.

public class OutClass {
    private String name="Zhang San";
    private static int age=18;

    public static void staticMethod(){
        System.out.println("outer staticMethod");
    }

    public void method(){
        System.out.println("outer method");
    }

    static class InClass{
        private static String hobby="dance";
        public static void dance(){
            System.out.println(new OutClass().name+"love"+hobby);
        }

        public void show(){
            //System.out.println(name);  error
            System.out.println("full name:"+new OutClass().name);
            System.out.println("Age:"+age);
            staticMethod();
            new OutClass().method();
            //method();  error
        }
    }

    public static void main(String[] args) {
        //System.out.println(InCLass.name2);  error
        InClass inClass=new InClass();
        inClass.show();
        InClass.dance();
    }
}
//Output results:
Name: Zhang San
 Age: 18
outer staticMethod
outer method
 Zhang Sanai dances

Posted by empnorton on Wed, 03 Nov 2021 13:38:15 -0700