Inner member of class 5: inner class

Inner member of class 5: inner class

  1. In Java, it is allowed to declare one class A in another class B, then class A is the inner class, and class B is called the outer class

  2. Classification of internal classes: member internal classes (static and non static) vs local internal classes (within methods, code blocks and constructors)

  3. Member inner class:

    1. On the one hand, as a member of an external class:
      >Calling the structure of an external class
      > can be modified by static
      >It can be modified by four different permissions

    2. On the other hand, as a class:
      >Properties, methods, constructors, etc. can be defined within a class
      >Can be modified by final to indicate that this class cannot be inherited. The implication is that you can inherit without using final
      >Can be modified by abstract

  4. Focus on the following three issues
    4. 1. How to instantiate an object of a member's inner class
    4. 2. How to distinguish the structure of calling external classes in member internal classes

public class InnerClassTest {
	public static void main(String[] args) {
		
		//Create a Dog instance (static member inner class):
		Person.Dog dog = new Person.Dog();
		dog.show();
		//Create a Bird instance (non static member inner class):
//		Person.Bird bird = new Person.Bird();// FALSE
		Person p = new Person();
		Person.Bird bird = p.new Bird();
		bird.sing();
		
		System.out.println();
		
		bird.display("Oriole");
	}
}

class Person{
	String name = "Xiao Ming";
	int age;
	
	public void eat(){
		System.out.println("Man: eat");
	}
	
	
	//Static member inner class
	static class Dog{
		String name;
		int age;
		
		public void show(){
			System.out.println("Carla is a dog");
//			eat();
		}
		
	}
	//Non static member inner class
	class Bird{
		String name = "cuckoo";
		
		public Bird(){}
		
		public void sing(){
			System.out.println("I am a little bird");
			Person.this.eat();//Calling non static properties of an external class
			eat();
			System.out.println(age);
		}
		
		public void display(String name){
			System.out.println(name);//Formal parameters of method
			System.out.println(this.name);//Properties of inner classes
			System.out.println(Person.this.name);//Properties of external classes
		}
	}
	
	public void method(){
		//Local inner class
		class AA{}
	}
	
	{
		//Local inner class
		class BB{}
	}
	
	public Person(){
		//Local inner class
		class CC{}
	}
}

Use of internal classes in development

public class InnerClassTest1 {
	//Rare in development
	public void method(){
	//Local inner class
		class AA{}
	}
	
	
	//Returns an object of a class that implements the Comparable interface
	public Comparable getComparable(){
		
		//Create a class that implements the Comparable interface: a local inner class
		//Mode 1:
		class MyComparable implements Comparable{
			@Override
			public int compareTo(Object o) {
			return 0;
			} 
		}
	return new MyComparable();
		
		//Mode 2:
		return new Comparable(){
			@Override
			public int compareTo(Object o) {
				return 0;
			}
		};
	}
}
  • 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.
  • In Java, the definition of one class is allowed to be located inside another class. The former is called internal class and the latter is called external class.
    Inner class is generally used within the class or statement block that defines it. When it is referenced externally, it must be given a complete name.
    • The name of the Inner class cannot be the same as the name of the external class containing it;
  • Classification: member inner classes (static member inner classes and non static member inner classes)
    Local inner classes (not to mention modifiers), anonymous inner classes

-Roles of member inner classes as members of classes:

  • Unlike external classes, inner classes can also be declared as private or protected;
  • You can call the structure of an external class
  • The Inner class can be declared as static, but the non static member variables of the outer class cannot be used at this time;
    -Roles of member inner classes as classes:
    • You can define structures such as properties, methods, constructors, etc. internally
    • It can be declared as an abstract class, so it can be inherited by other inner classes
    • Can be declared final
    • Generate outerclass $innerclass after compilation What's wrong with the festival (Ding Tongbu neishao dog is also used)
  • [note]
  1. Members in non static member inner classes cannot be declared static. Static members can only be declared in external classes or static member inner classes.
  2. The external class accesses the members of the internal class by means of "internal class. Member" or "internal class object. Member"
  3. Member inner classes can directly use all members of outer classes, including private data
  4. When you want to use an inner class in the static member part of an outer class, consider declaring the inner class static
  • 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. Anywhere else
      Cannot use this class
    • However, its object can be returned and used through the return value of external methods, and the return value type can only be a local internal class
      Parent class or parent interface type of
  • 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, but preceded by the class name and $symbol of the external class, as well as the numerical number.
    • 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.
    • Local inner classes can use members of outer classes, including private ones.
    • Local inner classes can use local variables of external methods, but they must be final. It is caused by different declaration cycles of local internal classes and local variables.
    • The status of local internal classes is similar to that of local variables. public,protected, default and private cannot be used
    • Local inner classes cannot be decorated with static, so they cannot contain static members
  • Anonymous Inner Class

    • Anonymous inner classes cannot define any static members, methods, or classes. Only one of the anonymous inner classes can be created
      Instances. An anonymous inner class must be behind new, which implicitly implements an interface or class.
    • Format:
      new parent class constructor (argument list) | implementation interface (){
      //The body part of an anonymous inner class
      }
    • Characteristics of anonymous inner classes
      • 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

Keywords: Java interface

Added by wiseoleweazel on Wed, 05 Jan 2022 09:20:43 +0200