当前位置:网站首页>Overview of famous inner classes and anonymous inner classes
Overview of famous inner classes and anonymous inner classes
2022-07-27 12:39:00 【Rippling rippling】
Inner class
Inner class : A class defined in an external class is called an internal class
Inner class :( classification )
1. Name the inner class
2. Anonymous inner class
3. Static inner class
4. Non static inner class
Name the inner class
Concept : A class with a name is called a named inner class .
namely :

Anonymous inner class
Concept : Classes that are not named at the beginning are called anonymous inner classes .
Because you need a class name to create an object , But anonymous classes are born without class names , So when anonymous inner class is defined, it is defined together with the created object .
grammar :
new Parent constructor ([ parameter list ])| Interface (){
// Anonymous inner class body
}
for example :
Object object = new Object() {
};
characteristic :
1、 Anonymous inner classes are General class Of Subclass
2、 Anonymous inner classes are abstract class Of Subclass
3、 Anonymous inner classes are Interface Of Implementation class
example 、
public class Mammal {
public void move () {
}
public void breath() {
}
public static void main(String [] args) {
Mammal r =new Mammal() {
@Override
public void breath() {
System.out.println("awesuft");
}
@Override
public void move () {
System.out.println("afky");
}
};
r.move();
r.breath();
}
}
Hold down Ctrl key , Click with the mouse r.move(); Of move. We can enter Mammal Of move In the method .
namely :r.move(); It's polymorphism .
Mammal r =new Mammal(){
}
This is an object of transformation . We can know new Mammal(){
} This is an anonymous inner class .Mammal Is his parent class name , And the definition of r yes Mammal class , So he is the object of transformation .
That is, it is proved that the anonymous inner class is a subclass of the ordinary class .
The same is true 2 and 3.
reflection :
An anonymous inner class cannot be an abstract class , Anonymous classes have no names , If it can be an abstract class , Then the anonymous class must have subclasses , But because there is no name , So there are no subclasses
summary :
Whether it's an anonymous inner class or a named inner class ,javac Will produce an independent class file : After the compilation, the inner class will be compiled to be independent .class file ,
If the inner class is Name the inner class , Then the bytecode file name of the inner class is the class name of the outer class +$+ Inner class class name ;
If Anonymous inner class , Then the anonymous inner class bytecode file name is the outer class class name +$+ Numbers ;
If you define an inner class directly in a class , It is used in a similar way to the member variable :
attach :
1、 External classes or external interfaces public default ; But the inner class defined directly in the class , There can be public ~private
In the method / You cannot add an access modifier to a code block .
private Decorated member variables can be used in inner classes
2、 If you use the value of a local variable in an inner class , be :JDK8.0 And above versions can be dispensed with final modification ,8.0 You have to use fianl
- Anonymous inner class No can Custom construction method , Because he doesn't have a class name ,
But you can initialize member variables through non static code blocks ; - Anonymous inner class No can Lower transformation , Because he doesn't have a class name
4. Anonymous inner classes can be defined in class , Method , Code block in .
5. You can add new properties and methods to anonymous inner classes , But these properties and methods cannot be called by the upper transformation object , It can only be called by anonymous inner class objects created in the way of non transformation objects
边栏推荐
- 2022 global Vocational Education Industry Development Report
- 12 pictures, take you to thoroughly understand ZGC garbage collector!
- Chapter 10 enumeration classes and annotations
- 20210419 combined sum
- POJ1988_ Cube Stacking
- Vi. analysis of makefile.build
- 5V boost 9V chip
- 2021-3-22-tencent - minimum number of guards
- Xposed+FDex2 app脱壳 (黑猫投诉app脱壳)
- js日期时间格式化(年月日、时分秒、星期、季度、获取时间差、日期与时间戳转换的功能)
猜你喜欢

BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)

js真伪数组转换

2021-3-22-directed graph sorting

How to use the server to build our blog

评价自动化测试优劣的隐性指标

XXL job parameter transfer

Will MySQL fail to insert data? Why?

HDU1698_Just a Hook

(10) STM32 - systick tick timer

Watermelon book chapter 3 (first & second)
随机推荐
Redis distributed online installation
2021-3-23-meituan-regular sequence
Uniapp video video playback is not completed. It is forbidden to drag the progress bar fast forward
堆
Openpyxl drawing area map
Alibaba cloud RDS exception during pool initialization
NFT mall /nft blind box / virtual blind box /nft transaction / customizable second opening
Plus SBOM: assembly line BOM pbom
V. introduction of other objectives and general options
Watermelon Book + pumpkin book chapter 1-2
4. Analysis of the execution process of make modules for general purposes
Openpyxl drawing radar map
Configuration files in MySQL
C program debugging and exception handling (try catch)
Implicit indicators for evaluating the advantages and disadvantages of automated testing
@Postconstruct annotations and initializingbean perform some initialization operations after bean instantiation
20210408 longest public prefix
虚拟偶像的歌声原来是这样生成的!
Photoshop web design tutorial
20210419 combined sum