10-Java inner class-member inner class, local inner class, anonymous inner class


1. Inner class

Define a class inside other classes, this class is called an inner class.

  • According to the internal class, the definition position is different, and it is divided into member internal class and local internal class
    • Member inner class : the inner class is located in the member position of the outer class
    • Local inner class : the inner class is located in the member method of the outer class

For example:

class A{
    
    
   //成员内部类:位于外部类的成员位置
   class B{
    
    

   }
    public void show(){
    
    
      //局部内部类,位于外部类成员方法内
       class C{
    
    

        }
    }
}

1. Member inner class

  • Access characteristics of member inner classes

    • The inner class can directly access the members of the outer class, including private ones.

    • To access the members of the inner class, the outer class must create an inner class object

    • You can create internal class objects directly outside, format

External class name. Internal class name Object name = new external class (). New internal class ()

  • note

    • The class cannot be privatized, but the internal class can (often used is the privatization of the internal class, the form of external indirect access to ensure data security)

    • Internal classes can be modified with static, called static internal classes, which can only access static members of external classes (for easy access to data)

    • The format of the static inner class directly created by the outside world

Wai.Nei a = new Wai.Nei ();

For example:

public class 内部类的访问特点 {
    
    
    public static void main(String[] args) {
    
    
        //可以在外部直接创内部类的对象
        Outer.Inner inner1=new Outer().new Inner();
        inner1.neishow();
        Outer outer=new Outer();
        outer.waishow();

    }

}
class Outer{
    
    
    int num=100;
    private int a=200;
    class Inner{
    
    
        int b=300;
        public void neishow(){
    
    
            System.out.println(b);
            //内部类可以发访问外部类的成员,包括私有成员
            System.out.println(num);
            System.out.println(a);
            waishow1();
        }
        public void neitest(){
    
    
            System.out.println("内部类方法执行");
        }
    }
    public void waishow(){
    
    
        System.out.println("外部的show方法执行");
        //外部类访问内部类的成员,必须创建内部类的对象
        Inner inner=new Inner();
        //通过内部类对象,访问内部类的成员变量
        System.out.println(inner.b);
        //通过内部类对象,访问内部类的成员方法
        inner.neitest();
    }
    private void waishow1(){
    
    
        System.out.println("外部类私有方法执行");
    }
}

2. Local inner class

  • Access characteristics of local internal classes
    • Can directly access members of external classes
    • You can create internal class objects and call internal class methods through objects to use internal class functions
    • Local internal class access to local variables must be final modification

For example:

public class 局部内部类 {
    
    
    public static void main(String[] args) {
    
    
        int a=100;
        int b=200;
        Outer outer=new Outer();
        outer.waishow(100,200);

    }
}
class Outer{
    
    
    private int num=20;

    public void waishow(final int a,final int b){
    
    
        //该局部变量需要使用final修饰
        final int num1=30;
        //局部内部类定义在成员方法内
        class inner{
    
    
            public void neishow(){
    
    
                num=40;
                System.out.println(num);
                System.out.println(num1);
                System.out.println(a+":"+b);
            }
        }
        //创建内部类对象,调用内部类功能
        inner x = new inner();
        x.neishow();
    }
}

3. Anonymous inner class

It is a simplified way of writing local inner classes.

  • Prerequisite: There is a class (can be an abstract class) or interface; format:

    new class name or interface name () { rewrite method; };

  • Nature:Anonymous objects that inherit this class or subclasses that implement this interface

For example:

//抽象类
abstract class AbstractClass{
    
    
    public abstract void show();
}
class Outer1{
    
    
    public void method(){
    
    
        //创建抽象类的子类的匿名对象
        new AbstractClass(){
    
    
            @Override
            public void show() {
    
    
                System.out.println("shoshowshow");
            }
        };
    }
}
//接口
public interface MyInter{
    
    
    public abstract void show();
}
class Outer{
    
    
    public void method(){
    
    
        new MyInter(){
    
    
            @Override
            public void show() {
    
    
                System.out.println("showshowshow");
            }
        };

    }
}

Guess you like

Origin blog.csdn.net/m0_46988935/article/details/110559846