Inner class grammar rules

Overview of internal classes:

An inner class means to define a class inside an outer class. As a member of the outer class, the inner class exists depending on the outer class.

##Internal categories are divided into the following categories:

1) Member inner class : As a member of the outer class, it exists alongside the properties and methods of the outer class.
2) Local inner class : The inner class defined in the method is called the local inner class. Similar to local variables, a local inner class cannot have an access specifier because it is not part of the outer class, but it can access the constants in the current code block and all members of this outer class.
3) Anonymous inner class : an inner class without a name. An anonymous inner class is a local inner class, so all restrictions on a local inner class take effect on it.
4) Static inner class : If there is no need to have a connection between the inner class object and the outer class object, then the inner class can be declared static, which is usually called a nested class (nested class)

Inner class grammar rules:

From the perspective of the external internal class, it can be completely regarded as a member of the external class, which is no different from ordinary members. The restrictions and modifications on ordinary members can be added to non-static internal classes. It's just that this member is no longer a basic data type, nor is it an object reference, but a class, and a class plays the role of a member.

Member inner class

Create an external class, DemoChengYuan, define the neibu internal class in the external class and define the internal class methods. Create a Main class to test it.

package 内部类;
public class DemoChengYuan {
    
    
    public class Neibu{
    
    	//定义一个成员内部类
        public void print(){
    
    //定义成员内部类方法
	System.out.println(“调用了成员内部类中的print方法”);
	}
    }
}
package 内部类;
public class Main {
    
    
public static void main(String[] args){
    
    	
        /**
	     *创建外部内部类对象
	     *外部类名.外部内部类名 name=new 外部类构造器.new 外部类构造器;
	     */ 
        DemoChengYuan.Neibu n1=new DemoChengYuan().new Neibu();
        n1.print();//调用外部内部类方法
        /**
	     *也可以使用下面方式创建外部内部类对象
	     *1、创建外部类对象
	     *2、外部类名.外部内部类名 name=外部对象.new 外部内部类构造器;
	     */
        DemoChengYuan demo=new DemoChengYuan();
        DemoChengYuan.Neibu n2=demo.new Neibu();
		n2.print();//调用外部内部类方法
    }
}

The interface is displayed as follows:

Insert picture description here

Local inner class

There are two advantages to using local inner classes.

  1. It is hidden from all external classes, even if the external class it belongs to, only the method in which it is located knows it.
  2. It can not only access data in the external class to which it belongs, but also access local variables, but local variables must be declared as final types.
    Because the local inner class is only valid locally, it can only access or create its objects in its valid location.
package 内部类;
public class DemoJuBu {
    
    
    final String AAA=”AAA”;
    public void dyAAA{
    
    	//外部类方法
        class JuBu{
    
    //定义局部类
	Public void print(){
    
     //定义局部类方法
	System.out.println(“调用了局部类的方法和常量AAA=+AAA);
		}
	}
        JuBu jb=new JuBu();//创建局部类对象
        jb.print();//创建局部类对象
    }
}
package 内部类;
public class Main {
    
    
public static void main(String[] args){
    
    	
        DemoJuBu jb=new DemoJuBu();	//创建外部对象
        jb.dyAAA();		//调用外部方法既调用了局部类
    }
}


The interface is displayed as follows:
Insert picture description here

.

Anonymous inner class

An anonymous inner class is an inner class without a name. An anonymous inner class has no name, so the anonymous inner class also creates an object when declaring the class.

package 内部类;
public class DemoNiMing {
    
    
    public void print(){
    
    	//定义外部类方法
        System.out.println(“这里是外部类方法”);
    }
}
package 内部类;
public class Main {
    
    
    public static void main(String[]args){
    
    	//定义外部类方法
        DomeNiMing nm=new DomeNiMing(){
    
    //创建外部对象,创建匿名类并创建其对象
	    public void print(){
    
    
		System.out.println(“这里是匿名类方法”);
	    }
	};
	        nm.print();//调用匿名类重写的方法	
    }
}

The interface is displayed as follows:Insert picture description here

Static inner class

When there is a static keyword before the name of an inner class, the inner class is a static inner class. The static inner class is a static member of the outer class, and it does not depend on the object of the outer class. Therefore, it is not necessary to create the object of the outer class first when creating a static inner class object outside the outer class. `

package 内部类;
public class DemoStatic {
    
    
    public static class Static{
    
    	//定义静态内部类
        public void staticprint(){
    
    
	    System.out.println(“这里是静态类方法”);
	}
    }
}
package 内部类;
public class Main {
    
    
public static void main(String[] args){
    
    	
        /**
	     *创建静态内部类对象
	     *外部类名.静态类名 name=new 外部类名. 静态类构造器;
	     */ 
        DemoStatic.Static ds=new DemoStatic.Static();
        Ds.staticprint();//调用静态类方法
        New DemoStatic.Static().staticprint();//外部类中使用静态类方法
    }
}

The interface is displayed as follows:Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45042462/article/details/114895425