当前位置:网站首页>什么是匿名内部类,如何使用匿名内部类
什么是匿名内部类,如何使用匿名内部类
2022-06-29 04:31:00 【zhang__1234】
- 匿名内部类既是一个类的定义,同时本身也是一个对象,它既有定义类的特征,也有创建对象的特征
class outer05{
private int n1=100;
public void f2(){
//基于类的匿名内部类
person person = new person(){
@Override
public void hi() {
System.out.println("重写了hi方法");
}
};
person.hi();//动态绑定 真正的运行类型是outer05$1
new person(){
@Override
public void say1(String name) {
super.say1(name);
}
}.say1("jack");/*可以不创建对象直接.say()*/
}
}
class person{
public void hi(){
}
public void say1(String name){
System.out.println("haha"+name);
}
}
- 可以直接访问外部类的所有成员,包括私有的
- 不能添加访问修饰符,因为它的地位就是一个局部变量
- 作用域:仅仅在定义它的方法或代码块
- 匿名内部类–访问–>外部类成员
- 外部其他类–不能访问–>匿名内部类
- 如果外部类和内部类的成员重名时,内部类访问的话,默认遵循就近原则,如果想访问外部类的成员,则可以使用(外部类名.this.成员)去访问
这篇文章解释的挺清楚的
https://blog.csdn.net/a850661962/article/details/109642780
边栏推荐
- Call snapstateon closed sou from Oracle CDC
- C language -- branch structure
- BERT和ViT简介
- 【Laravel系列8】走出 Laravel 的世界
- Installation and configuration of interrealsense d435i camera driver
- Analysis of moudo Network Library
- 热更新流程
- Remote connection of raspberry pie in VNC Viewer Mode
- iNFTnews | 元宇宙技术将带来全新的购物体验
- [结构力学] 结点承载下影响线与直接承载下影响线不同的原因
猜你喜欢

What are the circular statements of MySQL

1018 锤子剪刀布

How to use the select statement of MySQL

JSX的基本使用

Wi-Fi 7 来啦,它到底有多强?

Runtimeerror in yolox: dataloader worker (PID (s) 17724, 1364, 18928) exited unexpectedly

泰克DPO4104数字荧光示波器技术参数

Untitled

How to quickly install MySQL 5.7.17 under CentOS 6.5

直播预约|AWS Data Everywhere 系列活动
随机推荐
Airflow2.2.3 + efficiency + MySQL 8 build a robust distributed scheduling cluster
【HackTheBox】dancing(SMB)
CTO and programmer were both sentenced because the crawler was out of control!
String不同创建方式的区别
如何创建 robots.txt 文件?
What is the method of connection query in MySQL
How to display all MySQL databases
波形记录仪MR6000的实时波形运算功能
Memo pattern
Implementation of thread pool based on variable parameter template
我的创作纪念日
1019 digital black hole
NotImplementedError: Could not run torchvision::nms
汉泰示波器软件|汉泰示波器上位机软件NS-Scope,任意添加测量数据
Airflow 2.2.3 containerized installation
Mécanisme d'attention du canal de fixation
Gocd is good, but talk about Jenkins
Composite pattern
My creation anniversary
Ask a simple question about SQL