当前位置:网站首页>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
边栏推荐
- From "running distractor" to data platform, Master Lu started the road of evolution
- Jstack of JVM command: print thread snapshots in JVM
- Subghz, lorawan, Nb IOT, Internet of things
- matlab / ENVI 主成分分析实现及结果分析
- rt-thread 中对 hardfault 的处理
- A very good JVM interview question article (74 questions and answers)
- Talking about reading excel with POI
- 【GNN】图解GNN: A gentle introduction(含视频)
- 3428. 放苹果
- 那些自损八百的甲方要求
猜你喜欢

"Parse" focalloss to solve the problem of data imbalance

Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】

Redis(二)—Redis通用命令

屏幕程序用串口无法调试情况

博士申请 | 上海交通大学自然科学研究院洪亮教授招收深度学习方向博士生

为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾

ETCD数据库源码分析——从raftNode的start函数说起
![[FPGA tutorial case 13] design and implementation of CIC filter based on vivado core](/img/19/1a6d43c39f2cf810ba754ea9674426.png)
[FPGA tutorial case 13] design and implementation of CIC filter based on vivado core

Software testing knowledge reserve: how much do you know about the basic knowledge of "login security"?

693. 行程排序
随机推荐
ICML 2022 | explore the best architecture and training method of language model
3428. Put apples
Convert numbers to string strings (to_string()) convert strings to int sharp tools stoi();
高并发大流量秒杀方案思路
Test the foundation of development, and teach you to prepare for a fully functional web platform environment
屏幕程序用串口无法调试情况
dolphinscheduler3.x本地启动
A very good JVM interview question article (74 questions and answers)
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
一段程序让你明白什么静态内部类,局部内部类,匿名内部类
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
Vscode for code completion
Jstat pour la commande JVM: voir les statistiques JVM
Change the original style of UI components
计算模型 FPS
rt-thread 中对 hardfault 的处理
Experience sharing of contribution of "management world"
How to keep accounts of expenses in life
vim映射大K
SubGHz, LoRaWAN, NB-IoT, 物联网