当前位置:网站首页>About anonymous inner classes
About anonymous inner classes
2022-06-13 05:05:00 【Half a call】
Let's start with an example 


requirement : stay BB class AA Class object
Read this question carefully , You may find that he is very cunning
First , The two classes are under different packages . This is normal and common , The key is AA Class has only one constructor , and , His modifier is protected, Did you learn java Our friends should know ,protected The modifier represents that calls cannot be accessed under different packages
All the questions are coming , How to create this AA What about objects? ?
There are two solutions :
- Will be AA Modifier of constructor in class , Change to a wider range public
- Is to use today's focus


First , For the first method , This method is probably the one most people think of , This method is really direct violence , But , If you put AA Class transfer source code ? We have no way to modify the source code , All this approach has certain limitations , Not an option
then , That's the second way , Maybe it was a little strange at first 《 What the hell is this ?》
In another form, it may be clear and familiar 
This is actually our anonymous inner class
So the next question is , Why use the form of anonymous inner class ?
First , We have to make sure , This 
Or a AA Class ?

You can see , It is no longer a AA The class , So what is he ?
In fact, he looks like this , We should know that the object name of the anonymous inner class is not taken by us , The system helped us get it , All generally speaking, it looks a little strange 
In fact, he is AA A subclass of class
We can do it in AA Class to prove 


The fact is as we have proved , He's really a AA Subclasses of
Empathy , If AA Class is an interface, so its anonymous inner class should look like this 
OK, next question
That is why anonymous inner classes can call AA Class protected What about the constructor ?
We can refer to this structure and think about 
Since he is a AA Subclasses of classes , So what characteristics should he have ?
First , As a class , Without declaring a constructor , The system will assign an empty parameter constructor to the object by default
then , As a subclass , In the first line of the constructor , The system will also call the null parameter constructor of the calling parent class by default !!! It's called super()
therefore , In fact, he looks like this 
Because of the call super(), So he took it for granted to call the null parameter constructor of the parent class , That is to say AA Class protected Decorated null parameter constructor
Because the subclass calls the method attribute of the parent class, the method constructor should , All in this way , You have crossed the limit of the permission modifier
Empathy , If we pass in a parameter ( Or more ), But then , The system will not allocate null parameter constructors , Instead, you can directly call the constructor in the child or parent class that matches it 


边栏推荐
- C language learning log 12.14
- Several methods of identifying equivalent circuit of circuit drawing
- Reductive elimination
- C language learning log 2.19
- lookup
- MySQL8.0.13安装教程(有图)
- Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases
- Advanced C language - Section 1 - data storage
- RuoYi-Cloud启动教程(手把手图文)
- C language learning log 1.24
猜你喜欢

MySQL8.0.13安装教程(有图)

Binary search and binary answer

Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis

metaRTC4.0稳定版发布

Simple greedy strategy

Analysis on the usage, response and global delivery of provide/inject

Optocoupler working principle function electric parameter application circuit

Analysis of scoped attribute principle and depth action selector

Regular expressions in QT

Must know must know -c language keywords
随机推荐
Section 4 - arrays
The games that you've tasted
External sort
Chapter 17 free space management
Robot pose description and coordinate transformation
Bomb disposal cat
Several methods of identifying equivalent circuit of circuit drawing
metaRTC4.0集成ffmpeg编译
C language learning log 12.5
Section 7 - structures
Advanced C - Section 3 - character functions and string functions
On switch() case statement in C language
Embedded hardware - read schematic
Flex布局自适应失效的问题
Regular expressions in QT
关于匿名内部类
C language learning log 1.16
Win8.1和Win10各自的優勢
Sort (internal sort) + external sort
LeetCode第297场周赛(20220612)