The concept and classification of JavaSE 22 inner classes

Chapter 27 Concept and Classification of Inner Classes

If one thing is inside another thing, then it is a class inside another class.
Example: Relationship between car and engine

  • Classification:
  1. member inner class
  2. Local inner classes (including anonymous inner classes)

27.1 Inner classes with members

  • Format:
修饰符 class 外部类名称 {
    
    
	修饰符 class 内部类名称 {
    
    
		//...
	}
	//...
}
  • example:
public class Body {
    
    //外部类
	
	public class Heart {
    
    //成员内部类
	
		public viod beat() {
    
    //内部类方法
			System.out.println("beat,beat!");
			System.out.println(name);//正确;能够访问外部成员变量
		}
		
	}
	
	private String name;
	
	public void methodBody() {
    
    
		System.out.println("外部类的方法");
	}
	
	public void setName(String name) {
    
    
		this.name = name;
	}
	
	public String getName() {
    
    
		return name;
	}
	
}

Note: For internal and external use, you can access it at will; for external use and internal use, an internal class object is required.

27.1.1 Use of member inner classes

  1. Indirect way: In the method of the outer class, use the inner class; then main just calls the method of the outer class.
public class Body {
    
    //外部类
	
	public class Heart {
    
    //成员内部类
	
		public viod beat() {
    
    //内部类方法
			System.out.println("beat,beat!");
			System.out.println(name);//正确;能够访问外部成员变量
		}
		
	}
	
	private String name;
	
	public void methodBody() {
    
    
		System.out.println("外部类的方法");
		new Heart().beat();
	}
	
	public void setName(String name) {
    
    
		this.name = name;
	}
	
	public String getName() {
    
    
		return name;
	}
	
}
public class Demo1 {
    
    
	
	public static void main(String[] args) {
    
    
		Body body = new Body();
		//通过外部类的对象,调用外部类的方法,里面间接在使用内部类Heart
		body.methodBody();
	}
	
}
  1. Direct way: formula:外部类名称.内部类名称 对象名 = new 外部类名称().new 内部类名称();
public class Demo2 {
    
    
	
	public static void main(String[] args) {
    
    
		Body.Heart heart = new Body().new Heart();
		heart.beat();
	}
	
}

27.1.2 Same-named variable access in inner classes

  • Format:
    外部类名称.this.外部成员变量名
public class Outer {
    
    
	
	int num = 10;//外部类的成员变量
	
	public class Inner {
    
    
		int num = 20;//内部类的成员变量
	
		public void methodInner() {
    
    
			int num = 30;//内部类方法的局部变量
			System.out.println(num);//30局部变量;就近原则
			System.out.println(this.num);//20
//			System.out.println(super.num);//错误;内部类和外部类并不是继承关系
			System.out.println(Outer.this.num);//外部类的成员变量名
		}
	}
}
public class Demo {
    
    
	
	public static void main(String[] args) {
    
    
		Outer.Inner obj = new Outer().new Inner();
		obj.merhodInner();
	}
	
}

27.2 Definition of local inner classes

If a class is defined inside a method, then this is a local inner class.
"local":Only the method it currently belongs to can use it, out of this method, it cannot be used outside.

  • Definition format:
修饰符 class 外部类名称 {
    
    
	修饰符 返回值类型 外部类方法名称(参数列表) {
    
    
		class 局部内部类名称 {
    
    
			//...
		}
	}
}
  • example:
public class Outer {
    
    
	
	public viod methodOuter() {
    
    
		class Inner {
    
    	//局部内部类
			int num = 10;
			public void methodInner() {
    
    
				System.out.println(num);//10
			}
		}
		
		Inner inner = new Inner();
		inner.methodInner();	//只有当前方法才能访问
	}
	
}
public class Demo {
    
    

	public static void main(String[] args) {
    
    
		Outer obj = new Outer();
		obj.methodOuter();
	}

}

  • The permission modifier of the class in the next section:
    public > protected > (default) > private
    When defining a class, the permission modifier rules:
  1. External class: public / (default)
  2. Member inner class: public / protected / (default) / private
  3. Local inner class: (default)

27.2.1 The final problem of local inner classes

Local inner class, if you want to access the local variable of the method, then this local variable must be [valid final]

[Remarks]: Starting from Java 8+, as long as the local variables remain unchanged, the final keyword can be omitted

  • reason:
  1. The new object is in the heap memory.
  2. Local variables follow the method, in the stack memory.
  3. After the method finishes running, pop out of the stack immediately, and the local variables will disappear immediately.
  4. But new objects will persist in the heap until garbage collection disappears.
  5. As long as the local variable remains unchanged, it will be copied to the inner class as a constant.
public class MyOuter {
    
    

	public void methodOuter() {
    
    
		final int num = 10;//final可以省略但是num不能再重新赋值,否则局部类就不能访问
		
		class Inner {
    
    
			public void methodInner() {
    
    
				System.out.println(num);
			}
		}
	}
}

27.3 Anonymous inner classes

If the implementation class of the interface (or a subclass of the parent class) only needs to be used once. Then the definition of this class can be omitted under this kind of grabbing, and an anonymous inner class can be used instead.

  • Format: (Don't lose the colon at the end)
接口名称 对象名称 = new 接口名称() {
	//覆盖重写所有抽象方法
};
  • example
  1. normal method:
public interface MyInterface {
    
    
	
	/*public abstract*/ void method();//抽象方法
	
}
public class MyInterfaceImpl impelememts MyInterface {
    
    
	@Override
	public void method() {
    
    
		System.out.println("实现类覆盖重写了方法");
	}
}
public class Demo1 {
    
    
	public static void main(String[] args) {
    
    
		MyInterface obj = new MyInterfaceImpl();//多态
		obj.method();
	}
}
  1. anonymous inner class
public interface MyInterface {
    
    
	
	/*public abstract*/ void method();//抽象方法
	
}
public class Demo2 {
    
    

	public static void main(String[] args) {
    
    
		MyInterface obj = new MyInterface() {
    
    //这个大括号里就是个匿名内部类
			@Override
			public void method() {
    
    
				System.out.println("匿名内部类实现了方法A");
			}
		};
		obj.method();
		
		new MyInterface() {
    
    //使用方法2
			@Override
			public void method() {
    
    
				System.out.println("匿名内部类实现了方法B");
			}
		}.method();
	}
	
}

  • Format parsing: "new interface name() {...};"
  1. new represents the action of creating an object
  2. The interface name is which interface the anonymous inner class needs to implement
  3. {...} This is the content of the anonymous inner class
  • Precautions:
  1. Anonymous inner 创建对象classes can only be used once when . (If you want to create objects multiple times, and the content of the class is the same, you must use a separately defined implementation class)
  2. An anonymous 调用方法object can only be called once when . (If you want the same object, you need to give the object a name if you call it multiple times)
  3. Anonymous inner classes are omitted 实现类/子类名称, but anonymous objects are omitted 对象名称(anonymous inner classes and anonymous objects are not the same thing)

Guess you like

Origin blog.csdn.net/Niiuu/article/details/104317149