(JAVA): Detailed understanding of the four internal classes, text + code will be better understood - the focus of the third part

content

1. Member inner class ☞♥☜

ⅠBasic introduction☛♡☚

Ⅱ Usage details ☛♡☚

Ⅲ Code example demonstration ☛♡☚

2. Static inner class ☞♥☜

ⅠBasic introduction☛♡☚

Ⅱ Usage details ☛♡☚

Ⅲ Code example demonstration ☛♡☚

3. Local Internal Class ☞♥☜

ⅠBasic introduction☛♡☚

Ⅱ Usage details ☛♡☚

Ⅲ Code example demonstration ☛♡☚

 4. Anonymous inner class ☞♥☜

ⅠBasic introduction☛♡☚

Ⅱ Usage details ☛♡☚

Ⅲ Code example demonstration ☛♡☚


1. Member inner class ☞♥☜

ⅠBasic introduction☛♡☚

1. What is a member inner class?

        As a member of the external class, it is defined in the member position of the external class, and there is no static modification, and it is juxtaposed with the properties and methods of the external class. Instance variables of inner and outer classes can coexist.

2. Access method

        ①Access  instance variables in inner classes: this.property

        ② Access the instance variables of the outer class in the inner class: the outer class name.this.property

        ③ To access the inner class outside the outer class, use  out.inner.
  


Ⅱ Usage details ☛♡☚

1. As a member of the outer class, the inner class can directly access all members of the outer class, including private ones .

2. You can add any access modifier (public, protected, default, private), because the member inner class is equivalent to a member.

3. Member inner classes cannot define static members, only ordinary members or non-static members .

4. Note:

        ❶ In the outer class, inner s=new inner(); can be used directly, because the inner class is a member of the outer class, so the object can be generated.
        ❷ Outside of the outer class, to generate (new) an inner class object, you need to first create an outer class object (the outer class is available), and then generate an inner class object. Such as:
                
Outer o=new Outer();
                Outer.Inner in=o.new.Inner();

5. When creating an instance of a member inner class, an instance of the outer class must already exist.

6. When the inner class and the outer class have members with the same name, the member inner class can access the outer class members through the  outer class name.this.variable name  .


Ⅲ Code example demonstration ☛♡☚

(1) Create a simple member inner class

package com.Locally;

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

        //new一个外部类AA对象
        AA aa = new AA();

        //调用外部类的t1()方法
        aa.t1();

    }
}
class AA{

    //外部类的成员属性
    private int n1 = 10;
    public String name = "sminth";

    //定义成员内部类
    class BB{//类名

        //成员内部类的成员方法
        public void play(){
            //调用外部类的成员属性
            System.out.println("n1=" + n1 + " name=" + name);
        }

    }

    //外部类的成员方法    
    public void t1(){
        
        //new一个成员内部类
        BB bb = new BB();
        //调用成员内部类的play()方法
        bb.play();
    }

}

(II) The first access method of other external classes

package com.Locally;

public class Member {
    public static void main(String[] args) {
       
        //new一个外部类AA对象
        AA aa = new AA();

        //外部类使用成员内部类的第一种方式:    
        AA.BB b1 = aa.new BB();
        //解析:
        //AA.BB 表示外部类AA的内部类BB
        //aa.new BB() 可以看做是通过外部类对象(前面new外部类对象),去创建外部类中的内部类对象
        
        //然后直接调用内部类的play()方法
        b1.play();



    }
}
class AA{
    private int n1 = 10;
    public String name = "sminth";

    //定义成员内部类
    class BB{
        public void play(){
            System.out.println("n1=" + n1 + " name=" + name);
        }
    }

}

(iii) The second access method of other external classes

package com.Locally;

public class Member {
    public static void main(String[] args) {
        //new一个AA对象
        AA aa = new AA();
        aa.t1();

        //外部类使用成员内部类的第二种方式
       
        AA.BB b2 = aa.retu();
        //解析:
        //AA.BB 是外部类AA中的内部类BB
        //aa.retu() 等价于 aa.new BB()

        //直接调用内部类的play()方法
        b2.play();


    }
}
class AA{
    private int n1 = 10;
    public String name = "sminth";

    //定义成员内部类
    class BB{
        public void play(){
            System.out.println("n1=" + n1 + " name=" + name);
        }
    }

    //第二种:
    //定义一个外部成员方法tetu(),返回值为 内部类对象类型
    public BB retu(){
        return new BB(); //返回 内部类的new BB() 对象
    }
}

2. Static inner class ☞♥☜

ⅠBasic introduction☛♡☚

1. What is a static inner class

        A static class department is a member position defined in an outer class, similar to a member inner class. But the static inner class is modified with the static keyword, and the static inner class can only access the static members of the outer class.

2. Difference between static inner class and member class   

        Generating (new) a static inner class does not require outer class members, and the object of the static inner class can be generated directly, such as:
                
Outer.Inner in=new Outer.Inner();

        Instead of generating by generating outer class objects, private static inner classes can be defined.
      


Ⅱ Usage details ☛♡☚

1. You can directly access all static members of the outer class, including private ones, but you cannot directly access non-static members.

2. You can add any access modifier (public, protected, default, private), because the member inner class is equivalent to a member. Same as member inner class.

3. Static inner classes can define static members, as well as ordinary members or non-static members.

4. When the static inner class and the outer class have members with the same name, the static inner class can access the outer class members through the  outer class name and variable name  .


Ⅲ Code example demonstration ☛♡☚

(1) The first access method of other external classes

package com.Locally;

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

        //new 一个外部类Outer01 对象
        Outer01 outer01 = new Outer01();

        //外部类的第一种访问方式:直接访问静态内部类成员
        Outer01.Inner01 inner01 = new Outer01.Inner01();
        //解析:
        //Outer01.Inner01可以理解为创建外部类对象Outer01的内部类对象Inner01
        //Outer01.Inner01() 因为是静态的,所以直接访问

        //通过对象名,直接调用静态内部类成员
        inner01.cc();

        
    }
}
class Outer01{

    private static int n3 = 10;

    public void cpy(){
        System.out.println("这是cpy()方法");
    }
    
    //定义静态内部类
    static class Inner01{

        private int n2 = 33;
        
        public void cc(){
            System.out.println("n3=" + n3);

            //不能访问cpy(),因为它是普通成员,没有static 关键字修饰
        }
    }

    
}

(II) The second access method of other external classes

package com.Locally;

public class static_inner {
    public static void main(String[] args) {
        //定义外部类对象
        Outer01 outer01 = new Outer01();
        

        //其他外部类的第二种访问方式:
        //创建一个方法访问
        Outer01.Inner01 inner011 = outer01.n();
        //解析:
        //Outer01.Inner01 同上
        //outer01.n() 等价于 new Outer01.Inner01()

        //通过对象名直接访问静态内部类的成员
        inner011.cc();
    }
}
class Outer01{
    private static int n3 = 10;
    public void cpy(){
        System.out.println("这是cpy()方法");
    }
    
    //定义静态内部类
    static class Inner01{

        private int n2 = 33;

        public void cc(){
            System.out.println("n3=" + n3);
            
        }
    }

    //创建的方法
    //定义一个方法,返回值类型为 静态内部类对象
    public Inner01 n(){
        return new Inner01(); //返回 new了的静态内部类对象
    }
}

3. Local Internal Class ☞♥☜

ⅠBasic introduction☛♡☚

        What is a local inner class: A class defined in a member method or code block of a class is called a local inner class.

        Code Explanation:

class Outer{//外部类
    
    private int n1; //外部类属性
    public void m2(){} //外部类方法

    //外部类方法
    public void m1(){

        //方法中
        //局部内部类
        final class Inner{ //类名
            
            private int n1; //局部内部类属性

            public void f1(){ //局部内部类方法}
        }
    }

}

Ⅱ Usage details ☛♡☚

1. A local inner class is defined in the local location of the outer class, usually in the method

2. You can directly access all members of the outer class, including private

3. Access modifiers cannot be added, because its status is a local variable, and local variables cannot be modified by other modifiers, but can be modified by final.

4. Scope: only in the method or code block in which it is defined

5. If the members of the outer class and the local inner class have the same name, the principle of proximity is followed by default. If you want to access the members of the outer class, use the outer  class name.this.member name to access

6. The essence of the external class name.this is the object of the external class


Ⅲ Code example demonstration ☛♡☚

package com.Locally;

public class locally {
    public static void main(String[] args) {
        
        //new 一个外部类对象
        Outer outer = new Outer();
        outer.m1();
    }
}

class Outer{
    
    private int n1 = 100;

    public void m2(){
        System.out.println("Outer m2()");
    }

    public void m1(){
        
        //局部内部类
        final class Inner{

            private int n1 = 800;
            public void f1(){

                //  Outer02.this 本质就是外部类的对象,使用 外部类名.this.成员 去访问
                System.out.println("n1= " + n1 + " Outer.this.n1=" + Outer.this.n1);
                
                调用外部类的m2()方法
                m2();
            }
        }

        //new 一个局部内部类对象
        Inner inner = new Inner();
        //调用局部内部类的方法
        inner.f1();
    }

}

 4. Anonymous inner class ☞♥☜

ⅠBasic introduction☛♡☚

What is an anonymous inner class:

        An anonymous inner class is actually just an anonymous subclass, but it is hidden in the .class file and you cannot see it, so it is called an anonymous inner class!

Notice:

        An anonymous inner class is both a class definition and an object itself, so from a grammatical point of view, it has both the characteristics of defining a class and the characteristics of creating an object.


Ⅱ Usage details ☛♡☚

1. You can directly access all members of the outer class, including private

2. An access modifier cannot be added because its status is a local variable

3. Scope: only in the method or code block in which it is defined

4. If the members of the outer class and the anonymous inner class have the same name, the principle of proximity is followed by default. If you want to access the members of the outer class, use the outer  class name.this.member name to access


Ⅲ Code example demonstration ☛♡☚

package com.Locally;

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

        //new 一个Cellphone的对象
        Cellphone cellphone = new Cellphone();
        
        //匿名内部类
        cellphone.alarmclock(new Bell(){
            @Override
            public void ring() {
                System.out.println("System.out.println(\"懒猪起床了.....\");");
            }
        });

    }
}

//接口
interface Bell{
    void ring();
}

//定义一个类
class Cellphone{
    
    public void alarmclock(Bell bell){//传一个接口类的参数
        bell.ring();//动态绑定
    }

}

Anonymous inner class:

        cellphone.alarmclock(new Bell(){
            @Override
            public void ring() {
                System.out.println("System.out.println(\"懒猪起床了.....\");");
            }
        });

To resolve an anonymous inner class, it can be divided into two parts:

① First, create a new Bell() object and materialize it at the same time (that is, write properties or methods in {curly brackets}),

        Bell bell = new Bell(){
            @Override
            public void ring() {
                System.out.println("System.out.println(\"懒猪起床了.....\");");
            }
        }

②Call the alarmclock method of the cellphone class and pass a parameter bell

cellphone.alarmclock(bell);

③The anonymous inner class is to put the first step directly into the parameter position of the second step

④The call of the ring() method is called when bell.ring() in the alarmclock method of the cellphone class

Guess you like

Origin blog.csdn.net/yzh2776680982/article/details/124232342