当前位置:网站首页>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
边栏推荐
- dolphinscheduler3.x本地启动
- Database notes 04
- JVM monitoring and diagnostic tools - command line
- Jcmd of JVM command: multifunctional command line
- 骑士战胜魔王(背包&dp)
- vim映射大K
- Dc-7 target
- Cloud acceleration helps you effectively solve attack problems!
- Jinfo of JVM command: view and modify JVM configuration parameters in real time
- Three updates to build applications for different types of devices | 2022 i/o key review
猜你喜欢

matlab / ENVI 主成分分析实现及结果分析

Jcmd of JVM command: multifunctional command line

What are the classic database questions in the interview?

缓存在高并发场景下的常见问题

Laravel uses Tencent cloud cos5 full tutorial

693. Travel sequencing

Shared memory for interprocess communication

A very good JVM interview question article (74 questions and answers)

ETCD数据库源码分析——从raftNode的start函数说起

面试中有哪些经典的数据库问题?
随机推荐
Check point: the core element for enterprises to deploy zero trust network (ztna)
Several key steps of software testing, you need to know
jvm命令之 jcmd:多功能命令行
k8s运行oracle
如何解决数据库插入数据显示SQLSTATE[HY000]: General error: 1364 Field ‘xxxxx‘ doesn‘t have a default value错误
From "running distractor" to data platform, Master Lu started the road of evolution
Change the original style of UI components
Handling hardfault in RT thread
PostgreSQL database timescaledb function time_ bucket_ Gapfill() error resolution and license replacement
基本Dos命令
Solve pod install error: FFI is an incompatible architecture
「解析」FocalLoss 解决数据不平衡问题
3428. Put apples
雷特智能家居龙海祁:从专业调光到全宅智能,20年专注成就专业
POI导出Excel:设置字体、颜色、行高自适应、列宽自适应、锁住单元格、合并单元格...
Laravel uses Tencent cloud cos5 full tutorial
JVM monitoring and diagnostic tools - command line
string(讲解)
Convert numbers to string strings (to_string()) convert strings to int sharp tools stoi();
哈趣投影黑马之姿,仅用半年强势突围千元投影仪市场!