当前位置:网站首页>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
边栏推荐
- 生活中的开销,怎么记账合适
- Change the original style of UI components
- 3428. 放苹果
- k8s运行oracle
- 3428. Put apples
- 对称的二叉树【树的遍历】
- How to set up in touch designer 2022 to solve the problem that leap motion is not recognized?
- FlexRay通信协议概述
- 693. 行程排序
- Haqi projection Black Horse posture, avec seulement six mois de forte pénétration du marché des projecteurs de 1000 yuans!
猜你喜欢
外设驱动库开发笔记43:GPIO模拟SPI驱动
SubGHz, LoRaWAN, NB-IoT, 物联网
DC-7靶机
LM小型可编程控制器软件(基于CoDeSys)笔记二十三:伺服电机运行(步进电机)相对坐标转换为绝对坐标
win系统下安装redis以及windows扩展方法
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
Open the blue screen after VMware installation
Developers don't miss it! Oar hacker marathon phase III chain oar track registration opens
Deep clustering: joint optimization of depth representation learning and clustering
Jinfo of JVM command: view and modify JVM configuration parameters in real time
随机推荐
博士申请 | 上海交通大学自然科学研究院洪亮教授招收深度学习方向博士生
New Year Fireworks code plus copy, are you sure you don't want to have a look
3428. Put apples
SubGHz, LoRaWAN, NB-IoT, 物联网
蚂蚁庄园安全头盔 7.8蚂蚁庄园答案
Jstat pour la commande JVM: voir les statistiques JVM
ETCD数据库源码分析——从raftNode的start函数说起
When we talk about immutable infrastructure, what are we talking about
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
Go language learning notes - Gorm use - native SQL, named parameters, rows, tosql | web framework gin (IX)
Jmeter自带函数不够用?不如自己动手开发一个
360 Zhiyu released 7.0 new products to create an exclusive "unified digital workspace" for the party, government and army, and central and state-owned enterprises
Ant manor safety helmet 7.8 ant manor answer
微信小程序隐藏video标签的进度条组件
jvm命令之 jcmd:多功能命令行
直击2022ECDC萤石云开发者大会:携手千百行业加速智能升级
[FPGA tutorial case 14] design and implementation of FIR filter based on vivado core
Ctfshow-- common posture
2022Android面试必备知识点,一文全面总结
Deep clustering: joint optimization of depth representation learning and clustering