Java---Inner class

Java inner class

The Java language allows classes to be redefined within classes. Such classes defined inside other classes are called inner classes. The inner class exists in the outer class just like an instance member. There are four types of inner classes: static inner classes, instance inner classes, local inner classes, and anonymous inner classes.

  1. Static inner class
    When an inner class is decorated with static, we call it static nested class or static inner class.
package ZaiJiaYou;

class OuterClass1{
    private int a=10;
    private static int b=20;
    public OuterClass1(){
        System.out.println("进入外部类构造函数");
    }
    public void show(){
        System.out.println("进入外部类方法");
    }
static class InnerClass{//静态内部类
        private OuterClass1 outer=null;
        public InnerClass(OuterClass1 lzq){
            outer=lzq;
            System.out.println("进入静态内部类构造函数");
        }
        public void show2(){
            System.out.println(outer.a);//不能访问外部类的实例变量,必须通过传入后调用
            System.out.println(b);//可以直接访问外部类的静态成员变量
            System.out.println("内部类方法");
        }
    }
}

public class Day106 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub  
           OuterClass1 pwd=new OuterClass1();//用外部类去创建一个对象
           OuterClass1.InnerClass i=new OuterClass1.InnerClass(pwd);
           i.show2();
            }
    }

Output result:
write picture description here
Features of static inner class:

  • Can be instantiated independently of the outer class instance, the object of the inner class does not need the object of its outer class;
  • Non-static members of a static inner class can access the static variable n of the outer class and call static methods, but are not allowed to access the instance variable x and instance methods of the outer class;
  • Static inner classes can have static members, while non-static inner classes cannot have static members;
  • Static inner class creation object must depend on outer class to be createdOuterClass1.InnerClass i=new OuterClass1.InnerClass(pwd);
  • To access the instance variables of the external class, it must be called after passing in
private OuterClass1 outer=null;
System.out.println(outer.a);

2. Instance inner class

package ZaiJiaYou;

class OuterClass2{
    private int b=20;
    private static int c=30;
    private static final int d=40;
    //int    static int     static final int 
    public OuterClass2(){
        System.out.println("进入外部类构造方法~");
    }
    public void show(){
        System.out.println("进入外部类方法~");
    }
    class InnerClass{//实例内部类--->实例成员方法、变量
        private int data1=10;
        private int data2=20;
        //private static int data3=30;//error 实例内部类不能设静态成员?
        private static final int data4=30;//立即数
        //private static final int data5=data1+30;//error
        public InnerClass(){
            System.out.println("进入内部类构造方法~");
        }
        public void show(){
            //System.out.println(a);
            System.out.println(b);
            System.out.println(c);

            System.out.println("data1:"+this.data1);//
            //System.out.println("outer data1:"+OuterClass2.this.data1);
            System.out.println(data2);
            System.out.println("进入内部类方法~");
        }
    }
}
public class Day107 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        OuterClass2 outer=new OuterClass2();
        //实例化  一个实例内部类
        OuterClass2.InnerClass in=outer.new InnerClass();
        in.show();//
    }

}

Output result:
write picture description here
The characteristics of the instance inner class:

  • The inner class can access all the members of the outer class as it wants to access its own members without restrictions;
  • This in the inner class refers to the instance object of the inner class itself. If you want to use the instance object of the outer class, you can use the class name.this to get it;
  • The inner class object cannot have static members for the simple reason that the instance object of the inner class is a member of the instance object of the outer class.
    Output order: static –> instance –> constructor

  • Each instance of an inner class must have an outer class object that depends on the outer class object:

OuterClass2 outer=new OuterClass2();
        //实例化  一个实例内部类
        OuterClass2.InnerClass in=outer.new InnerClass();

3. Local inner class: can be defined in a method or even a code block.

package Practice;
/*
 *本地内部类
 * 
 * */
class Student{
    private int age;
    private String name;
    //public static int data1=10;//方法区
    public int data2=20;

    public Student(){
        System.out.println("进入外部类构造函数1~");
    }
    public Student(String name){
        this.name=name;
        System.out.println("进入外部类构造函数2~");
    }
    public void show(){
        //static int d=20;//static 定义的一定是类得而数据成员
        System.out.println("name:"+name+"age:"+age+"data2:"+data2);
    }

    //本地内部类:方法内部的类
    public void test(){//可以是 static 
        final int data6=10;//局部变量
        //本地内部类  只能访问拥有这个内部类方法的被  final 修饰的变量
        class InnerClass{
            private int a=10;
            //private static int b=20;
            private static final int c=20;//立即数

            public InnerClass(){
            }
            public void show(){
                System.out.println("data6:"+data6);
                System.out.println("进入内部类方法~");
            }
        }
        InnerClass in=new InnerClass();
        in.show();
    }
}

public class Practiceb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Student s2=new Student();
        s2.show();
    }

}

The output result is: It
write picture description here
should be noted that: the inner class in the method, but this inner class can only be called internally but not in the outer class.

4. Anonymous inner class: The Java language allows to define the implementation of the class while creating an object, but the class name is not specified, and Java defines it as an anonymous inner class. An anonymous inner class in Java is the same as an anonymous array. When only an object of a class needs to be created and its name is not used, or some inner classes only need to create an object of it, this class will not be used in the future. In this case, it is generally more appropriate to use anonymous inner classes. The syntax rules for anonymous inner classes are as follows:
new interfacename(){…};
or new superclassname(){…};

package Practice;
/*
 *匿名内部类
 * */
class Student{
    private int age;
    private String name;
    //public static int data1=10;//方法区
    public int data2=20;

    public Student(){
        System.out.println("进入外部类构造函数1~");
    }
    public Student(String name){
        this.name=name;
        System.out.println("进入外部类构造函数2~");
    }
    public void show(){
        //static int d=20;//static 定义的一定是类得而数据成员
        System.out.println("name:"+name+"age:"+age+"data2:"+data2);
    }
}
public class Practicea {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

            new Student(){//匿名内部类相当于   当前类的子类
                    public void show(){
                        System.out.println("进入匿名内部类~");
                    }
                }.show();

            }
        }

Output result:
write picture description here

Note: An anonymous inner class has no constructor because it has no name (but if this anonymous inner class inherits a parent class that only has a constructor with parameters, it must be created with these parameters and implemented in the use the super keyword in the process to call the corresponding content). If you want to initialize its data members, you can use the following methods:

  • If it is an anonymous inner class of a method, pass the required parameters through this method, and these parameters must be declared final;
  • Transform an anonymous inner class into a named local inner class, so that this class can have a constructor;
  • Inside the anonymous inner class is an initialization block.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485747&siteId=291194637