Introduction to the most complete internal classes of the whole network

Posted by Anarking on Mon, 22 Nov 2021 14:47:52 +0100

Internal class: as the name suggests, internal class is a class nested inside other classes. It can be divided into four types according to the location and modified keywords. They are: member inner class, static inner class, method inner class and anonymous inner class.

(1) Member inner class: the usage is similar to that of member method. The definition is used inside the outer class and can be modified with private. However, it should be noted that the outer class cannot be modified with private.

public class Outter {
        private int age;
        private String name;
    //Member inner class, private can modify inner class, but cannot modify outer class.

 class Inner{
        private String msg = "hello Inner Class";

        public void test() {
            System.out.println(msg);
            //Private domain that can access external classes
            System.out.println(age);
        }
    }
}

Features: 1. Internal classes can be modified with private, but it should be noted that external classes cannot be modified with private.

2. Internal class and external class are private domains that can access each other, and their internal class can directly access the private domain of external class. Because there are hidden external objects inside the member class. See the code block above for the implementation. The external class accessing the private domain of the internal class must be accessed through the object of the internal class.

   public void fun(){               
        //Member methods of external classes
        //Create an internal class object
        Inner inner = new Inner();
        System.out.println(inner.msg);

    }

3. The inner class can solve the single inheritance limitation of java in a disguised form.

class A{
    protected int age = 11;
}
class B{
    protected String msg = "hello world";
}
public class Inherit {

    public static void main(String[] args){
        Inherit inherit = new Inherit();
        inherit.fun();

    }
    class InnerClassA extends A{}

    class InnerClassB extends B{}

    public void fun(){
        InnerClassA a = new InnerClassA();
        InnerClassB b = new InnerClassB();
        System.out.println(a.age);
        System.out.println(b.msg);

    }


}

4. Internal classes can be hidden from the outside.

---------------Feature 2.3.4 is the meaning of the existence of internal classes-------------------------------------------------------------------- Relationship between internal class and external class:

1. The creation of internal classes of members depends on external class objects. Analogy member methods must be called through objects. Internal class objects cannot be created without external class instances.

2. Internal class and external class are relatively independent.

3. The private attributes between the two visit each other.

4. Syntax for creating internal classes:

(1) . create an inner class object inside the outer class

        Inherit inherit = new Inherit();

(2) . create an inner class object outside the outer class (the inner class is not modified by private)

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

5. Use the hidden external class object (this) inside the internal class

 class Inner{
        private String msg = "hello Inner Class";
        private int age = 20;
        public void test() {
            System.out.println(msg);
            //Private domain that can access external classes
            System.out.println(Outter.this.age);
            System.out.println(this.age);
        }
    }

**************************************************************************************************************

Note: the inner class of a member cannot have a static domain, but it can access the static domain of the outer class.

(2) Static inner class: defined inside the outer class and decorated with static.

Features: 1. The static internal class can be used without the generation of external class objects, but it cannot access the member domain of the external class and can access the static domain of the external class.

package innerclass;

public class StaticInnerClass {
    static int age;
    String name;
    static class Inner{
        public void test(){
            System.out.println(age);
            System.out.println(name);
        }
    }
}

As can be seen from the above example, the static internal class can access age normally, but cannot access name normally, and an error will appear after running.

  2. Static inner classes can have their own member domains.

3. Comparison between static inner class and member inner class:

Member internal class ▬ must have external class object ▬ analogy member method ▬ can access external static domain, but cannot own static domain.

Static inner class ▬ independent of external class objects ▬ analogy to static methods ▬ can own member domains, but cannot access member domains of external classes (except static modification)

Static inner class creation syntax:

 StaticInnerClass.Inner inner = new StaticInnerClass.Inner()

(3) Method inner class: defined inside a method

package innerclass;

public class FunClass {
    public void fun(){
        //9 method internal class local variable
        int a = 23;
        class Inner{
            
        }
    }
}

characteristic:

1. It is completely hidden from the outside, so the inner class of the method cannot have any access modifiers, which is meaningless.

2. If you want to use the formal parameters of the method, you can only use it and cannot change it.

 

a. When the class inside the method does not access the formal parameter, the formal parameter can be modified at will in the method.

b. The class inside the method uses a formal parameter, which must be declared final

(4) Anonymous inner class: defined in the method, the class has no name.

characteristic:

1. It is often used in multithreading.

2. You must inherit an abstract class or implement an interface.

3. There is no construction method, which can be destroyed once used.

The results are as follows:

package innerclass;
interface IMessage{
    void fun();
}//Interface
public class NoNameClass {
    public static void main(String[] args) {
        IMessage msg = new IMessage() {
            //Anonymous Inner Class 
            @Override
            public void fun() {
                System.out.println("hello world");
            }
        };
        msg.fun();
    }
}

Topics: Java