Internal classes of the five members of the [JavaSE] [object-oriented 06] class

Posted by medaswho on Thu, 30 Dec 2021 13:09:29 +0100

Class: Inner Class

1. Why have inner classes

1. Internal scenario
When there is an internal part of a thing that needs a complete structure to describe, and the internal complete structure only provides services for external things, it is best to use the internal class for the whole internal complete structure.
2. Internal features
Declared as an inner class, you can use it directly to access all members of the outer class, including private ones

3. Source code example:
The iterator of JRE core class library set uses a lot of internal classes; Each collection implementation class has an iterator inner class

2. Classification of internal classes

(1) Static inner class
(2) Non static inner class:

  • Member inner class
  • Local inner class
  • Anonymous Inner Class

3. Member inner class

(1) How to declare member inner classes

Declared location: outside method in class
The format is as follows:

[Modifier ] class External class{
 	[Modifier ] class Member inner class{
 	}
 }

(2) Characteristics of member internal classes

  • The inner class is still a separate class. After compilation, the inner class will be compiled into a separate class Class file, but preceded by the class name of the external class and the $symbol. For example: outer $inner class
  • The internal class of a member is also a member of an external class. All permission modifiers public, protected, default and private can be used
  • Member inner classes can also be decorated with final and abstract
  • Member inner classes cannot contain static members
  • Member inner classes can directly use all members of outer classes, including private ones.
  • If the internal class of a member has the same name as the non static attribute of the external class, it can be distinguished by "external class name. this. Attribute". If it has the same name as the static attribute of the external class, it can be distinguished by "external class name. Class variable"

(3) How to use member inner classes

1. Use member inner class in outer class

  • Non static member inner classes cannot be used in static members of external classes
  • In the non static members of the external class, directly create the objects of the internal class to access the properties and methods of the internal class. At this point, it can be regarded as an ordinary class

2. Use the member inner class outside the outer class

  • An object of an external class is required to create an object of a member's internal class
package com.innerclass.member;

public class TestMemberClass {

	public static void main(String[] args) {
		Outer out = new Outer();
		out.outerMethod();
		
		Outer.Inner oi = out.new Inner();
		oi.innerMethod();
		
		Outer.Inner obj = out.getInner();
		obj.innerMethod();
	}

}
class Outer{
	private int value = 5;
	public static void outerStaticMethod(){
		/*Inner in = new Inner();//error
		in.innerMethod();*/
	}
	public void outerMethod(){
		System.out.println("Methods of external classes");
		/*Inner in = new Inner();//sure
		in.innerMethod();*/
	}
	class Inner{
		public void innerMethod(){
			System.out.println("Methods of inner classes");
			System.out.println("Methods of the inner class access private members of the outer class:"+value);
		}
	}
	//Returns the object of the inner class through a method of the outer class
	public Inner getInner(){
		return new Inner();
	}
}

4. Static inner class

(1) How to declare static inner classes

Declared location: outside method in class
The format is as follows:

[Modifier ] class External class{
 	[Modifier ] static class Static inner class{
 	}
 }

(2) When to declare a static inner class

  • When an inner class needs to contain static members
  • When you want to use an inner class in the static member part of an outer class

(3) Characteristics of static inner classes

  • The inner class is still a separate class. After compilation, the inner class will be compiled into a separate class Class file, but preceded by the class name of the external class and the $symbol. For example: outer $inner class
  • Static inner classes can use modifiers public, protected, default and private
  • Static inner classes can also be decorated with final and abstract
  • Static inner classes can contain static members
  • Static inner classes can directly use static members of external classes, including private ones. You cannot use non static members of an external class.
  • If the static internal class has the same name as the static attribute of the external class, if you want to represent the attribute of the external class, use "external class name. Attribute"

(4) How to use static inner classes

  • Use static inner classes in outer classes: just like other ordinary classes
    • In a static member of an external class
    • In a non static member of an external class
  • Use member inner classes outside the outer class:
    • Use static members of static inner classes: external class names Static internal class name Static members of static inner classes
    • Use non - static members of static inner classes: external class names Static internal class name obj = new external class name Static internal class name ();
      obj. Non static members of static inner classes
package com.innerclass.staticinner;

public class TestStaticInnerClass {

	public static void main(String[] args) {
		Outer.StaticInnerClass.innerStaticMethod();
		Outer.StaticInnerClass os = new Outer.StaticInnerClass();
		os.innerMethod();
	}

}
class Outer{
	public static void outerStaticMethod(){
		StaticInnerClass.innerStaticMethod();
		StaticInnerClass si = new StaticInnerClass();
		si.innerMethod();
	}
	public static void outerMethod(){
		StaticInnerClass.innerStaticMethod();
		StaticInnerClass si = new StaticInnerClass();
		si.innerMethod();
	}
	static class StaticInnerClass{
		public static void innerStaticMethod(){
			System.out.println("Static methods of inner classes");
		}
		public void innerMethod(){
			System.out.println("Non static methods of inner classes");
		}
	}
}

5. Local inner class

(1) How to declare local inner classes

Declared location: in a method or code block of an external class
The format is as follows:

[Modifier ]  class External class{
 	method{
			class Local inner class{
				}
		}
}

(2) Characteristics of local inner classes

  • The inner class is still a separate class. After compilation, the inner class will be compiled into a separate class Class file "class name of external class $numeric number, internal class name. Class".
  • The status of local internal classes is similar to that of local variables. Member modifiers such as public, protected, private and static cannot be used, but abstract or final can be used
  • Local inner classes cannot be decorated with static, so they cannot contain static members.
  • It can only be used in the method or code block that declares it, and it is declared before use. You can't use this class anywhere else because of the scope problem.
  • Local inner classes can use members of outer classes, including private ones. However, whether non static members of external classes can be used depends on whether the method is non static.
  • Local inner classes can use local variables of external methods, but they must be final. It is caused by the different life cycles of local internal classes and local variables.

(3) How to use local inner classes

  • It can only be used in the method or code block that declares it, and it is declared before use. You cannot use this class anywhere else.
  • However, its objects can be returned and used through the return value of external methods. The return value type can only be the parent class or parent interface type of the local internal class.
package com.innerclass.local;

public class TestLocalInnerClass {

	public static void main(String[] args) {
		Outer out = new Outer();
		Object obj = out.outerMethod();
		System.out.println(obj);
	}

}
class Outer{
	int value = 5;
	public Object outerMethod(){
		final int localValue = 10;
		class LocalInnerClass{
			public void innerMethod(){
				System.out.println("Methods of local inner classes");
				System.out.println("Methods of local internal classes can use members of external classes:"+value);
				System.out.println("Methods of local internal classes can use local variables of external classes:"+localValue);
			}
		}
		//Declare before use
		LocalInnerClass li = new LocalInnerClass();
		li.innerMethod();
		return li;
	}
}

6. Anonymous inner class

(1) How to declare anonymous inner classes

Declared location: in any statement that can create an object
The format is as follows:

new Parent class/Parent interface(){
	....
}

It means to create an object that implements (inherits) the class of the interface (parent class).

(2) Characteristics of anonymous inner classes

  • Anonymous inner class is a class. Its compiled bytecode file name is: external class name $numeric number class
  • Anonymous inner classes must inherit from parent classes or implement interfaces
  • An anonymous inner class can only have one object
  • Anonymous inner class objects can only be referenced in polymorphic form
  • Anonymous inner classes are special local inner classes to which all the restrictions of local inner classes apply

(3) How to use anonymous inner classes

Three methods of use:

  • Inheritance
  • Interface type
  • Parametric formula
package com.innerclass.anonymous;

import java.util.Arrays;
import java.util.Comparator;

public class TestAnonymous {

	public static void main(String[] args) {
		//Inheritance
		Car car = new Car(250){
			public void run(){
				System.out.println("Improved:" + getSpeed());
			}
		};
		
		Car[] cars = new Car[3];
		cars[0] = car;
		cars[1] = new Car(120);
		cars[2] = new Car(140);
		
		for (Car c : cars) {
			c.run();
		}
		
		//Interface type
		Comparator com = new Comparator() {

			@Override
			public int compare(Object o1, Object o2) {
				Car c1= (Car) o1;
				Car c2= (Car) o2;
				return c1.getSpeed() - c2.getSpeed();
			}
		};
		
		Arrays.sort(cars,com);
		
		//Parametric formula
		Arrays.sort(cars,new Comparator() {

			@Override
			public int compare(Object o1, Object o2) {
				Car c1= (Car) o1;
				Car c2= (Car) o2;
				return c1.getSpeed() - c2.getSpeed();
			}
		});
		
		
		for (Car c : cars) {
			c.run();
		}
	}

}
class Car{
	private int speed;
	
	public Car() {
		super();
	}

	public Car(int speed) {
		super();
		this.speed = speed;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public void run(){
		System.out.println("Standard engine run-up" + speed);
	}
}

Topics: Java JavaSE