博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中的匿名内部类总结
阅读量:7011 次
发布时间:2019-06-28

本文共 2025 字,大约阅读时间需要 6 分钟。

匿名内部类也就是没有名字的内部类

正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写

但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口

 

实例1:不使用匿名内部类来实现抽象方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
abstract 
class 
Person {
    
public 
abstract 
void 
eat();
}
 
class 
Child
extends 
Person {
    
public 
void 
eat() {
        
System.out.println(
"eat something"
);
    
}
}
 
public 
class 
Demo {
    
public 
static 
void 
main(String[] args) {
        
Person p =
new 
Child();
        
p.eat();
    
}
}

运行结果:eat something

可以看到,我们用Child继承了Person类,然后实现了Child的一个实例,将其向上转型为Person类的引用

但是,如果此处的Child类只使用一次,那么将其编写为独立的一个类岂不是很麻烦?

这个时候就引入了匿名内部类

 

实例2:匿名内部类的基本实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
abstract 
class 
Person {
    
public 
abstract 
void 
eat();
}
 
public 
class 
Demo {
    
public 
static 
void 
main(String[] args) {
        
Person p =
new 
Person() {
            
public 
void 
eat() {
                
System.out.println(
"eat something"
);
            
}
        
};
        
p.eat();
    
}
}

运行结果:eat something

可以看到,我们直接将抽象类Person中的方法在大括号中实现了

这样便可以省略一个类的书写

并且,匿名内部类还能用于接口上

 

实例3:在接口上使用匿名内部类

interface 
Person {
    
public 
void 
eat();
}
 
public 
class 
Demo {
    
public 
static 
void 
main(String[] args) {
        
Person p =
new 
Person() {
            
public 
void 
eat() {
                
System.out.println(
"eat something"
);
            
}
        
};
        
p.eat();
    
}
}

运行结果:eat something

 

由上面的例子可以看出,只要一个类是抽象的或是一个接口,那么其子类中的方法都可以使用匿名内部类来实现

最常用的情况就是在多线程的实现上,因为要实现多线程必须继承Thread类或是继承Runnable接口

 

实例4:Thread类的匿名内部类实现

public 
class 
Demo {
    
public 
static 
void 
main(String[] args) {
        
Thread t =
new 
Thread() {
            
public 
void 
run() {
                
for 
(
int 
i =
1
; i <=
5
; i++) {
                    
System.out.print(i +
" "
);
                
}
            
}
        
};
        
t.start();
    
}
}

运行结果:1 2 3 4 5

 

实例5:Runnable接口的匿名内部类实现

1
2
3
4
5
6
7
8
9
10
11
12
13
public 
class 
Demo {
    
public 
static 
void 
main(String[] args) {
        
Runnable r =
new 
Runnable() {
            
public 
void 
run() {
                
for 
(
int 
i =
1
; i <=
5
; i++) {
                    
System.out.print(i +
" "
);
                
}
            
}
        
};
        
Thread t =
new 
Thread(r);
        
t.start();
    
}
}

运行结果:1 2 3 4 5

 

 

 

 

实例6:Thread 类的匿名使用,直接传Runnable接口的匿名内部类实现

1
2
3
4
5
6
7
8
9
10
11
public 
class 
Demo6{
    
public 
static 
void 
main(String[] args) {
        
new 
Thread(
new 
Runnable() {
            
public 
void 
run() {
                
for 
(
int 
i =
1
; i <=
5
; i++) {
                    
System.out.print(i +
" "
);
                
}
            
}
        
}).start();
    
}
}

 

转载于:https://www.cnblogs.com/longhs/p/3754529.html

你可能感兴趣的文章
拼团代付时出现缺少字段问题,添加字段的SQL语句
查看>>
Python 编写知乎爬虫实践
查看>>
CSharp基础知识2-选择语句
查看>>
wiki 的搭建和使用
查看>>
Linux下DRBD配置
查看>>
IP多播初步见解
查看>>
nginx屏蔽HEAD、PUT等请求
查看>>
Technical explanation of The MySpace Worm
查看>>
新闻列表标签
查看>>
消息队列技术点梳理(思维导图版)
查看>>
iOS9搜索功能说明
查看>>
PVS让存储颤抖,系列博文之四:PVS的写缓存新技术之XenApp方式实测篇
查看>>
Java调用python脚本以及会出现的问题
查看>>
Linux下使用命令获取redis数据脚本
查看>>
Java输入输出流 3
查看>>
书摘-团队
查看>>
The Apply method of function object
查看>>
利用for与if写出你的第一个小脚本
查看>>
多个ElasticSearch Cluster的一致性问题
查看>>
深入浅出Swarm
查看>>