当前位置:网站首页>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
边栏推荐
- Shared memory for interprocess communication
- 那些自损八百的甲方要求
- matlab / ENVI 主成分分析实现及结果分析
- From "running distractor" to data platform, Master Lu started the road of evolution
- What are the classic database questions in the interview?
- JMeter function assistant - random value, random string, fixed value random extraction
- 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
- DC-7靶机
- JVM命令之 jstat:查看JVM統計信息
- JVM monitoring and diagnostic tools - command line
猜你喜欢
软件测试的几个关键步骤,你需要知道
JVM监控及诊断工具-命令行篇
A very good JVM interview question article (74 questions and answers)
[FPGA tutorial case 14] design and implementation of FIR filter based on vivado core
SubGHz, LoRaWAN, NB-IoT, 物联网
Three updates to build applications for different types of devices | 2022 i/o key review
一段程序让你明白什么静态内部类,局部内部类,匿名内部类
测试开发基础,教你做一个完整功能的Web平台之环境准备
字符串常量与字符串对象分配内存时的区别
Experience sharing of contribution of "management world"
随机推荐
[shell] summary of common shell commands and test judgment statements
Bypass open_ basedir
Experience of Niuke SQL
安装mongodb数据库
How to keep accounts of expenses in life
MySQL(十)
Talking about reading excel with POI
POI excel export, one of my template methods
高并发大流量秒杀方案思路
如何解决数据库插入数据显示SQLSTATE[HY000]: General error: 1364 Field ‘xxxxx‘ doesn‘t have a default value错误
window下面如何安装swoole
Haqi projection Black Horse posture, avec seulement six mois de forte pénétration du marché des projecteurs de 1000 yuans!
Developers don't miss it! Oar hacker marathon phase III chain oar track registration opens
外设驱动库开发笔记43:GPIO模拟SPI驱动
骑士战胜魔王(背包&dp)
QT console output in GUI applications- Console output in a Qt GUI app?
Software testing knowledge reserve: how much do you know about the basic knowledge of "login security"?
FlexRay通信协议概述
Change the original style of UI components
缓存在高并发场景下的常见问题