当前位置:网站首页>A program lets you understand what static inner classes, local inner classes, and anonymous inner classes are
A program lets you understand what static inner classes, local inner classes, and anonymous inner classes are
2022-07-07 06:24:00 【Pei Nanwei_】
An inner class is a class defined in another class . Why use inner classes ? The main reasons are as follows :
- Internal class methods can access the data in the scope where the class definition is located , Include private The data of .
- Inner classes can be hidden from other classes in the same package .
- When you want to define a callback function and don't want to write a lot of code , It is convenient to use anonymous inner class .
Next, let's take the code as an example
public class Test {
// Static inner class
static class C implements A {
@Override
public void lambad() {
System.out.println(" I'm a static inner class ");
}
}
public static void main(String[] args) {
A object = new B();
object.lambad();
// Static inner class way
object = new C();
object.lambad();
// Local inner classes
class D implements A {
@Override
public void lambad() {
System.out.println(" I'm a local inner class ");
}
}
object = new D();
object.lambad();
// Anonymous inner class There is no class name , Must implement the interface or parent class
object = new A() {
@Override
public void lambad() {
System.out.println(" I'm an anonymous inner class ");
}
};
object.lambad();
}
}
// Define a functional interface
interface A {
void lambad();
}
// Implementation class
class B implements A {
@Override
public void lambad() {
System.out.println(" I am a general implementation class ");
}
}
Okay , That's all for this article , Favorite students can like the collection , Have a problem , Can comment , Or leave a message , I will give you feedback at the first time , Thank you for watching. !!
notes : This article is for me to share my learning experience , There are mistakes or areas that need to be corrected , Please correct me. , I will accept with an open mind
边栏推荐
- SubGHz, LoRaWAN, NB-IoT, 物联网
- jvm命令之 jcmd:多功能命令行
- 【GNN】图解GNN: A gentle introduction(含视频)
- Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
- 3531. 哈夫曼树
- 港科大&MSRA新研究:关于图像到图像转换,Fine-tuning is all you need
- 谷歌 Chrome 浏览器发布 103.0.5060.114 补丁修复 0-day 漏洞
- Database notes 04
- LM小型可编程控制器软件(基于CoDeSys)笔记二十三:伺服电机运行(步进电机)相对坐标转换为绝对坐标
- uniapp开发小程序如何使用微信云托管或云函数进行云开发
猜你喜欢
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
Ant manor safety helmet 7.8 ant manor answer
Subghz, lorawan, Nb IOT, Internet of things
string(讲解)
「解析」FocalLoss 解决数据不平衡问题
Three updates to build applications for different types of devices | 2022 i/o key review
如何在Touch Designer 2022版中设置解决Leap Motion不识别的问题?
【GNN】图解GNN: A gentle introduction(含视频)
CloudCompare-点对选取
ETCD数据库源码分析——从raftNode的start函数说起
随机推荐
A freshman's summary of an ordinary student [I don't know whether we are stupid or crazy, but I know to run forward all the way]
How to solve sqlstate[hy000]: General error: 1364 field 'xxxxx' doesn't have a default value error
3428. Put apples
Apple CMS V10 template /mxone Pro adaptive film and television website template
Go language learning notes - Gorm use - native SQL, named parameters, rows, tosql | web framework gin (IX)
vim映射大K
Database notes 04
软件测试的几个关键步骤,你需要知道
Niuke Xiaobai monthly race 52 E. sum logarithms in groups (two points & inclusion and exclusion)
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
牛客小白月赛52 E.分组求对数和(二分&容斥)
Redisl garbled code and expiration time configuration
哈趣投影黑馬之姿,僅用半年强勢突圍千元投影儀市場!
Ant manor safety helmet 7.8 ant manor answer
Several key steps of software testing, you need to know
Haqi projection Black Horse posture, avec seulement six mois de forte pénétration du marché des projecteurs de 1000 yuans!
ICML 2022 | explore the best architecture and training method of language model
安装VMmare时候提示hyper-v / device defender 侧通道安全性
[SOC FPGA] peripheral PIO button lights up
360织语发布7.0新品 为党政军、央国企打造专属“统一数字工作空间”