JavaSE - day8 inner class

Problems with interface types as formal parameters

1) Provided to the sub-implementation class of the interface

2) The way of inner class


Here is 2) to expand the writing.

What is an inner class?

Inner class As the name implies, a class that exists inside a class is called an inner class.

Features of inner classes

Inner classes can directly access all , including private ones.
If the inner class needs to be accessed by the outer class, then the outer class needs to create the object of the inner class for indirect access. (Inner classes seem to understand outer classes better...)
The outer class wants to access the member methods of the inner class (non-static inner class):
    Format: outer class name. inner class name object name = outer class object. inner class object
    example:
//Requirement: Access member methods in inner classes
class Outer{
	int a = 10 ;
	class Inner{
		public void show() {
			System.out.println(a);//The inner class can directly access the member location of the outer class
		}
	}
}
public class TestDemo1 {
	public static void main(String[] args) {
		Outer.Inner s = new Outer().new Inner();
		s.show();
	}
}

Classification of inner classes

Inner classes can be further divided into:
  Member inner class: The inner class's member position in the outer class.
  Member outer class: The inner class is local to the outer class (in a method of the outer class).

Modifiers inside members

Private can guarantee security! We know that external classes can create internal objects, which is unsafe for internal classes. Internal classes modified by private cannot be created by external classes.
static can treat a static inner class as a member of the outer class.

anonymous inner class

new class name or interface name {
 
    method override();
  }
The essence of anonymous inner class: it inherits the class or implements the subclass object


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325849989&siteId=291194637