当前位置:网站首页>Internal class (detailed explanation of four internal classes)
Internal class (detailed explanation of four internal classes)
2022-07-27 21:42:00 【Xiao Tang learns to catch babies】
== = = = = = = Inner class ( Detailed explanation of four internal classes )= = = = = = = ==
One 、 Basic introduction : Another class structure is completely nested inside a class . Nested classes are called inner classes (inner class), Classes that nest other classes are called outer classes (outer class). Is the fifth largest member of our class 【 reflection : What are the five members of a class ?[ attribute 、 Method 、 Constructors 、 Code block 、 Inner class ]】, The biggest feature of internal classes is that they can directly access private properties , And it can reflect the inclusion relationship between classes , Be careful : Internal classes are the difficulty of learning , It's also the point , When looking at the underlying source code later , There are a lot of inner classes .
If the definition class is in a local location ( In the method / Code block ) :(1) Local inner classes (2) Anonymous inner class
Defined in the member location (1) Member inner class (2) Static inner class
Two 、 Basic grammar
class Outer{ // External class
class Inner{ // Inner class
}
}
class Other{ // External other classes
}
3、 ... and 、 Classification of internal classes
Defined in the local location of the external class ( As in method ):
1) Local inner classes ( Class name )
2) Anonymous inner class ( No class name , a key !!!)
· Defined in the member position of the external class :
1) Member inner class ( useless static modification )
2) Static inner class ( Use static modification )
1、 Local inner classes
explain : A local inner class is defined in a local location of an outer class , For example, in the method , And have a class name .
1. You can directly access all members of an external class , Include private
2 Cannot add access modifier , Because its status is a local variable . You can't use modifiers for local variables . But you can use final modification , Because local variables can also be used final
3. Scope : Just in the method or code block that defines it .
4. Local inner classes — visit ----> Members of external classes [ access : Direct access ]
5. External class — visit ----> Members of local inner classes
access : Create objects , Revisit ( Be careful : Must be in scope )
remember :
(1) Local inner classes are defined in methods / Code block
(2) The scope is in the method body or code block
(3) The essence is still a class
6. External other classes — Cannot access -----> Local inner classes ( Because the local internal class status is a local variable )
7. If the members of the external class and the local internal class have the same name , By default, follow the principle of proximity , If you want to access members of an external class , You can use ( External class name .this. member ) To visit
8. External classes in methods , You can create Inner class object , Then you can call the method
2、 Anonymous inner class
(1) The essence is class
(2) Inner class
(3) This class has no name
(4) It's also an object
explain : Anonymous inner classes are defined in outer classes
Local location of , For example, in the method , And no class name
1. The basic syntax of anonymous inner classes
new Class or interface ( parameter list ){
The class body
};
① The syntax of anonymous inner classes is strange , Please pay attention , Because the anonymous inner class is both the definition of a class , At the same time, it is also an object itself , So in terms of grammar , It has the characteristics of defining classes , There are also features of creating objects ,, So you can call anonymous inner class methods .
②. You can directly access all members of an external class , Include private
③ Cannot add access modifier , Because its status is a local variable
④ Scope : Just in the method or code block that defines it .
⑤ Anonymous inner class — visit ----> External class members [ access : Direct access ]
⑥ External other classes — Cannot access -----> Anonymous inner class ( Because anonymous inner class status is a local variable )
⑦ If the members of the outer class and the anonymous inner class have the same name , Anonymous inner class access , By default, follow the principle of proximity , If you want to access members of an external class , You can use ( External class name .this. member ) To visit
3、 Member inner class
explain : The inner class of a member is defined in the member position of the outer class , And there's no static modification .
1. You can directly access all members of an external class , Include private
2. You can add any access modifier (public、protected、 Default 、private), Because its land
Bit is a member .
3. The scope is the same as other members of the external class , For the entire class body , Create a member inner class object in the member method of the outer class , Call the method again .
4. Member inner class — visit ----> External class members ( such as ; attribute )[ access : Direct access ]
5. External class — visit ------> Member inner class access : Create objects , Revisit
6. External other classes — visit ----> Member inner class
7. If the members of the outer class and the inner class have the same name , Internal class access , By default, follow the principle of proximity , If you want to access members of an external class , You can use ( External class name .this. member ) To visit
4、 Static inner class
explain : The static inner class is defined in the member position of the outer class , And there are static modification
1. You can directly access all static members of the external class , Include private , But you can't directly access non static members
2. You can add any access modifier (public、protected、 Default 、private), Because its status is a member .
3. Scope : With other members , For the entire class body
4. Static inner class — visit ----> External class ( such as : Static attribute )[ access : Direct access to all static members ]
5. External class — visit ------> Static internal class access mode : Create objects , Revisit
6. External other classes — visit -----> Static inner class
7. If the members of the external class and the static internal class have the same name , When static inner classes are accessed , By default, the nearest... Is followed
principle , If you want to access members of an external class , You can use ( External class name . member ) To visit
边栏推荐
- 零钱通项目(两个版本)含思路详解
- 看起来是线程池的BUG,但是我认为是源码设计不合理。
- Simple manual implementation of map
- MySQL执行过程及执行顺序
- In crsctl, the function of displayed home
- Ziguang zhanrui: dozens of 5g terminals based on chunteng 510 will be commercially available in 2020
- Software testing interview question: what is regression testing?
- LM NAV: robot navigation method based on large models of language, vision and behavior
- ORA-27300,ORA-27301,ORA-27302,ORA-27303,TNS-2518,TNS-12549,TNS-12560,TNS-00519等告警处理
- LInkedList底层源码
猜你喜欢

看起来是线程池的BUG,但是我认为是源码设计不合理。

Box model and element positioning

STL源码剖析

监听服务器jar运行,及重启脚本

腾讯云[HiFlow】| 自动化 -------HiFlow:还在复制粘贴?

为什么要使用MQ消息中间件?这几个问题必须拿下

Technical practice behind bloom model: how to refine 176billion parameter model?

Report design - how to make your powerbi Kanban brilliant?

@RequestParam注解的详细介绍

CBAM learning notes
随机推荐
2021-11-05 understanding of class variables and class methods
微软商店无法下载应用,VS2019无法下载插件问题解决方案
Openai issued a document to introduce the latest application of Dall · E 2: fully enter the field of artistic creation and design
排序(冒泡排序)后面学习持续更新其它排序方法
How to realize a good knowledge management system?
Recursion / backtracking (Part 1)
@Autowired注解与@Resource注解的区别
@Detailed introduction of requestparam annotation
Worthington phospholipase A2 study phosphatidylcholine 2-acetylhydrolase
异常-Exception
UNITY--读取Excel
Idea connects to MySQL database and performs SQL query operations
自研5G芯片商用推迟?未来4年苹果iPhone都将采用高通5G芯片
Characteristics and determination scheme of Worthington mushroom polyphenol oxidase
Acwing3715. 最少交换次数(冒泡排序法的模拟思路)
技术管理 - 一定要抓大放小
Daily Mathematics Series 60: February 29
疫情之下,手机供应链及线下渠道受阻!销量骤降库存严重!
数组扩容、排序、嵌套语句应用
Some operations about Anaconda (installing software and quickly opening)