JavaSE-Inner Class and Lambda Expression (11)

1. Concept
  1. Inner class: Inside a class, a complete class is defined.

    ​ class Outer{ // external class

    ​ class Inner{} // inner class

    ​ }

  2. After the internal class is compiled, a separate .class file will be generated. The naming method of the .class file is:

    The class name of the outer class $ the class name of the inner class.class

  3. The inner class can directly access the members of the outer class; usually the inner class is constituted as a component of the outer class

  4. Classification of internal classes: member internal class, static internal class, local internal class, anonymous internal class

2. Member internal classes (analogous to instance variables) [Understand: Recognize when reading the source code]
  1. Location: Defined inside the class, outside the method

  2. The creation of the inner class object of the member: the creation of the object dependent on the outer class:

    Outer o = new Outer(); // Create an object of external class

    Outer.Inner i = o.new Inner(); // Create an object of member inner class

  3. The inner class of the member can directly access the properties and methods of the outer class (private can also be accessed)

  4. The class name of the external class. This: represents the object of the current external class

    The class name of the external class.this.attribute: Access the properties of the external class

    The class name of the external class. This. member method (actual parameter);: access to the member method of the external class

  5. Member inner classes cannot define static members (static properties, static methods, static code blocks)

public class TestInner {
    
    
	public static void main(String[] args) {
    
    
		
		Outer o = new Outer();
		
		// 创建成员内部类的对象:必须依赖于 外部类的对象而创建
		
		Outer.Inner i = o.new Inner();
		System.out.println(i.n);
		i.inner_method();
		
		
		System.out.println(o.a);
		
	}
}

// 外部类
class Outer{
    
    
	
	int a = 3; // 实例变量
	// 成员方法
	public void outer_method() {
    
    
		System.out.println("外部类的成员方法....");
	}
	
	// 成员内部类(类比于实例变量)
	class Inner{
    
    
		int n = 7;
		int a = 6; // 成员内部类中属性
		//static int m = 2;
		public Inner() {
    
    }
		public void inner_method() {
    
    
			int a = 8;// 局部变量
			System.out.println("内部类中成员方法....");
			System.out.println("a="+a);
			System.out.println("this.a="+this.a);                       System.out.println("Outer.this.a="
                                         +Outer.this.a);
			Outer.this.a = 10;
		}
	}
}
Three, static inner class (analogous to: static variable) [Understand: when reading the underlying source code]
  1. Location: Defined inside the class, outside the method, the inner class modified by static

  2. Instance variables and static variables can be defined in static inner classes (the same is true for methods)

  3. Create objects of static inner class:

    External class class name. Static internal class class name reference name = new external class class name. Static internal class name ();

  4. Access to static members of static inner classes: you can access directly through the class name

    External class class name. Static inner class class name. Static properties: access to static properties of static inner classes

    External class class name. Static internal class class name. Static method (actual parameter); method of accessing static internal class

  5. The static inner class can only directly access the static members of the outer class (static properties and static methods);

    Non-static members of the outer class cannot be directly accessed in the static inner class.

    public class TestStaticInner {
          
          
    	public static void main(String[] args) {
          
          
    		// 创建静态内部类的对象
    		/*
    		 * MyClass.Inner i = new MyClass.Inner();
    		 * 
    		 * System.out.println(i.m); i.inner_method();
    		 */
    		//i.inner_static_method();
    		System.out.println(MyClass.Inner.n);
    		
    		MyClass.Inner.inner_static_method();
    	}
    }
    
    class MyClass{
          
          
    	int a = 2; // 实例变量
    	static int b = 5; // 静态变量
    	
    	public void outer_method() {
          
          
    		System.out.println("外部类的成员方法...");
    	}
    	
    	public static void outer_static_method() {
          
          
    		System.out.println("外部类的静态方法....");
    		System.out.println("b="+b);
    	}
    	// 静态内部类
    	static class Inner{
          
          
    		int m = 1;// 实例变量
    		static int n = 2; // 静态变量
    		
    		public void inner_method() {
          
          
    			System.out.println("静态内部类中成员方法....");
    			System.out.println("b="+b);
    		}
    		public static void inner_static_method() {
          
          
    			System.out.println("静态内部类的静态方法...");
    		}
    	}
    }
Fourth, local internal classes (analogous to: local variables) [Occasionally see the source code, transitional use]
  1. Location: Defined in functions and methods

  2. To create an object of a local internal class, you need to complete the creation of the object inside the method that defines it, and the object should be created after the class definition (first define the class, then create the object)

  3. Note: Local inner classes can be defined in member methods or static methods.

  4. Local inner classes can directly access outer class members (static members + non-static members)

    Note: At this time, the inner class is defined in the member method.

  5. The local inner class can access the local variable inside the method that defines it, but the local variable must be final modified before it can be accessed by the local inner class.

    JDK8.0 version and above, if JVM detects that a local variable in a method is a local internal class method inside this method, jvm defaults to adding final in front of this local variable, this syntax is called syntactic sugar

public class TestLocalInner {
    
    
	public static void main(String[] args) {
    
    	
		  ClassA ca = new ClassA();  
		  ca.outer_method();
	}
}
class ClassA{
    
    
	int n = 3;// 实例变量
	static int f = 8;
	
	public void outer_method() {
    
    
		System.out.println("外部类的成员方法....");
		
		int m = 6; // 局部变量
		System.out.println("m="+m);
		
		// 局部内部类
		class Inner{
    
    
			int a = 1;	
			public void inner_method() {
    
    
				System.out.println("局部内部类的成员方法...");
				System.out.println("n="+n);
				System.out.println("m="+m); // final 
			}
		}
		// 局部内部类的对象
		Inner i =new Inner();
		System.out.println(i.a);
		i.inner_method();
	}
	public void test() {
    
    
//		System.out.println("m="+m);
	}
}
5. Anonymous inner class [optional content according to your own ability-simplified code]
  1. Anonymous inner class: is a special local inner class

  2. Features:

    (1) The definition of an anonymous inner class must inherit a parent class or implement an interface

    (2) The definition of the anonymous inner class is completed with the creation of the object; and based on an anonymous inner class

    ​ Only one object of this class can be created

  3. grammar:

    interface IA{
          
          	
       void m1();
    
       void m2();
    }
    // 匿名内部类实现一个接口
    IA ia = new IA(){
          
          
         public void m1(){
          
           // 方法实现的部分 }
    
         public void m2(){
          
           // 方法实现的部分 }
    };
  4. The advantages and disadvantages of anonymous inner classes:

    (1) Disadvantages: poor readability

    (2) Advantages: reduce the amount of code and improve coding efficiency

public class TestAnonymousInner {
    
    
	public static void main(String[] args) {
    
    
		// 匿名内部类
		IA ia=new IA() {
    
    
			public void m1() {
    
    
				System.out.println("匿名内部类覆盖了m1方法");
			}
		};
		ia.m1();
		ia.m1();
		
		// 继承一个父类定义一个匿名内部类
		ClassD cd = new ClassD() {
    
    
			public void m1() {
    
    
				System.out.println("子类覆盖了m1方法.....");
			}
		};
		cd.m1();
		cd.m2();
        
        test(new IA(){
    
    
            public void m1(){
    
    
                System.out.println("实现部分...");
            }
        });
	}
    public static void test(IA ia){
    
    
        ia.m1();
    }
}

interface IA{
    
    
	void m1();
}

class ClassD{
    
    
	public void m1() {
    
    
		System.out.println("父类中m1方法");
	}
	public void m2() {
    
    
		System.out.println("父类中m2方法....");
	}
}
6. Lambda expressions (applications starting with JDK8.0) [This content is optional according to your own capabilities]
  1. Lambda expression: It is a simplified form of anonymous inner class that implements functional interface and completes the creation of objects.

Insert picture description here

  1. Application scenario: When there is only one abstract method in the interface (the static method and the default method are not among them)

  2. grammar:

    接口名 引用 = (形参列表) -> {
          
           // 方法实现部分 };
    
    语法解释: -> 是JDK8.0新引入一个语法符号
       -> 左侧:Lambda表达式参数,即接口方法中形参部分
       -> 右侧:指定Lambda表达式执行功能部分,即接口方法中实现部分 

    (1) No parameters, no return value

    ​ a. Grammatical structure: interface name reference = () -> {// method implementation part};

    ​ b. Note: When there is only one line in {}, {} can be omitted, but it is not recommended.

    public class TestAnonymousInner3 {
          
          
    	public static void main(String[] args) {
          
          
    		IC ic = () -> {
          
          
    			System.out.println("这是方法实现部分...");
    		};
    		ic.mc();
    	}
    }
    interface IC{
          
          
    	void mc();
    }

    (2) With parameters and no return value

    ​ a. Syntax structure: interface name reference = (data type variable name, data type variable) -> {// method implementation part);

    ​ b. Note: The data type of the parameter list of the Lambda expression can be omitted, and the compiler infers it by itself.

    public class TestAnonymousInner3 {
          
          
    	public static void main(String[] args) {
          
          
    		ID id = (a, s) ->{
          
          
    			System.out.println("a="+a);
    			System.out.println("s="+s);
    		};
    		id.md(2, "小龙");
    	}
    }
    interface ID{
          
          
    	void md(int a,String str);
    }

    (3) With return value:

    ​ a. Syntax structure: interface name reference name = (parameter list)->{ return xxx; };

    ​ b. Note: If there is only one return statement in {}, the return statement and {} can be omitted (together).

    public class TestAnonymousInner3 {
          
          
    	public static void main(String[] args) {
          
          	
    		IE ie = (int a,int b) -> {
          
          
    			return a+b;
    		};
    		System.out.println(ie.me(3, 5));
            
            // 如果Lambda{}实现中只有 return 语句
            IE ie2 = (int a,int b) -> a+b;
    		System.out.println(ie2.me(3, 5));
    	}
    }
    interface IE{
          
          
    	int me(int a,int b);
    }

Guess you like

Origin blog.csdn.net/Java_lover_zpark/article/details/105236141