Java4Android self-study records (23): inner classes and anonymous inner classes

The road is hindered and long, and the line is coming. Keep your head down and work hard, if you don't talk, you will be a blockbuster! Come on, Sao Nian!

Directory and resource index

  Java4Android self-learning process catalog and resource index

1 What is an inner class?

  Inner class: A class is defined inside another class, so this class is called an inner class; the code example is as follows:

class A
{
    
    
    class B
    {
    
    
        
	}
}

  If you compile directly at this time, what will be the result of the compilation? There will be 2 class files after compilation, as shown below:
image-20200526224829631

  Among them, A$B.class is the class file generated by the inner class;

  The compiled name of the inner class is:External class name$Internal class name.class

2 What are the grammatical characteristics of inner classes?

2.1 How to generate internal class objects?

  Still use the above class file, at this time create a new class to store the main function, the program content is as follows:

class Test
{
    
    
	public static void main(String args[]) 
	{
    
    
		A a = new A();				// 生成类A 对象
		
		A.B b = new A().new B();	// 生成子类B 对象
	}
}

2.2 How to use internal class member variables/functions

  We first modify the class file in the first section and add some member variables to it for testing. The specific code is as follows:

class A 
{
    
    
	int i;
	
	class B 
	{
    
    
		int j;
		
		int funB()
		{
    
    
			int result = i + j;
			System.out.println(result);		// 添加result打印,调试用
			return result;
		}
	}
}

  At this time, modify the main function, assign values ​​to the member variables in class A and class B, and view the output result of the function

class Test
{
    
    
	public static void main(String args[]) 
	{
    
    
		A a = new A();				// 生成类A 对象
		
		A.B b = a.new B();			// 生成子类B 对象
		
		a.i = 3;
		b.j = 1;
		b.funB();
	}
}

  After the above code is compiled and run, the result is as follows:
image-20200526230400661

  From the analysis of the code and running results:

  • The inner class can use the member variables defined by the outer class at will.
  • B is an internal class of A, which means that member variables and member functions in A can be used in B at will, but this does not mean that B inherits A, but can be used, but B does not own these member variables.
  • Every object of an inner class is associated with an object of an outer class. In other words, if there is an internal class object, there must be an external class object associated with it.
  • The variable i actually used in the inner class B is the member variable in the outer class.

3 Anonymous inner class

  Anonymous inner class: the first is the inner class, and then there is no name, it is called the anonymous inner class;

  Use specific code below to implement, create a new interface class A

interface A 
{
    
    
	public void doSomething();
}

  Then define a class B

class B 
{
    
    
	public void fun(A a)
	{
    
    
		System.out.println("B类的fun函数");
		a.doSomething();
	}
}

  At this time, class B needs an object of type A. As a parameter, it also needs a subclass of interface A, because A cannot directly generate objects, as follows:

class AImpl implements A 
{
    
    
	public void doSomething()
	{
    
    
		System.out.println("doSomething");
	}
}

  The main function is slightly modified at this time, the specific code is as follows:

class Test
{
    
    
	public static void main(String args[]) 
	{
    
    
		AImpl al = new AImpl();	// 生成子类对象
		A a = al;				// 向上转型
		
		B b = new B();
		b.fun(a);				// 传进去类型为A的参数
	}
}

  The compilation and running results of the above code are as follows:
image-20200526232124996

  Note that if anonymous inner classes are used at this time, there will be an unusual way of writing. Modify the main function code as follows:

class Test
{
    
    
	public static void main(String args[]) 
	{
    
    
		// AImpl al = new AImpl();	// 生成子类对象
		// A a = al;				// 向上转型
		
		B b = new B();
		b.fun(new A()
			{
    
    
				public void doSomething()
				{
    
    
					System.out.println("匿名内部类");
				}
			}
		);
	}
}

  After the above code is recompiled, the running result is as follows:

  The analysis of anonymous internal class code and compilation results is as follows:

  • The fun function of class B requires an object of type A, which is basically similar to the previous AImpl, but without a name;
  • new A() cannot be used directly, it must follow the class that implements the interface, but without a name, it is called an anonymous class;

4 summary

  1. Understand inner classes, basic grammatical characteristics of inner classes, implementation methods, etc.;
  2. Understand the usage and scope of internal class member variables and member functions;
  3. A brief understanding of anonymous internal classes, characteristics and implementation methods.

If the content of the article is wrong, please comment / private message a lot of advice, thank you! If you think the content of the article is not bad, leave a like, your like is my biggest encouragement, thank you!

Guess you like

Origin blog.csdn.net/Fighting_Boom/article/details/106368055