当前位置:网站首页>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
边栏推荐
- Symmetric binary tree [tree traversal]
- VIM mapping large K
- Oracle迁移中关于大容量表使用数据泵(expdp、impdp)导出导入容易出现的问题和注意事项
- Ant manor safety helmet 7.8 ant manor answer
- Redis(二)—Redis通用命令
- SubGHz, LoRaWAN, NB-IoT, 物联网
- Crudini profile editing tool
- You don't know the complete collection of recruitment slang of Internet companies
- C语言面试 写一个函数查找两个字符串中的第一个公共字符串
- Handling hardfault in RT thread
猜你喜欢

FlexRay通信协议概述

Test the foundation of development, and teach you to prepare for a fully functional web platform environment

JMeter function assistant - random value, random string, fixed value random extraction

Shared memory for interprocess communication

Rk3399 platform development series explanation (WiFi) 5.53, hostapd (WiFi AP mode) configuration file description

jvm命令之 jcmd:多功能命令行

Convert numbers to string strings (to_string()) convert strings to int sharp tools stoi();

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

如何在Touch Designer 2022版中设置解决Leap Motion不识别的问题?

Software testing knowledge reserve: how much do you know about the basic knowledge of "login security"?
随机推荐
c面试 加密程序:由键盘输入明文,通过加密程序转换成密文并输出到屏幕上。
Niuke Xiaobai monthly race 52 E. sum logarithms in groups (two points & inclusion and exclusion)
Rk3399 platform development series explanation (WiFi) 5.53, hostapd (WiFi AP mode) configuration file description
牛客小白月赛52 E.分组求对数和(二分&容斥)
JVM monitoring and diagnostic tools - command line
jmeter 函数助手 — — 随机值、随机字符串、 固定值随机提取
string(讲解)
Crudini 配置文件编辑工具
[shell] summary of common shell commands and test judgment statements
Three updates to build applications for different types of devices | 2022 i/o key review
"Parse" focalloss to solve the problem of data imbalance
蚂蚁庄园安全头盔 7.8蚂蚁庄园答案
c语言面试写一个函数在字符串N中查找第一次出现子串M的位置。
rt-thread 中对 hardfault 的处理
Markdown displays pictures side by side
进程间通信之共享内存
高并发大流量秒杀方案思路
当我们谈论不可变基础设施时,我们在谈论什么
ETCD数据库源码分析——从raftNode的start函数说起
Change the original style of UI components