java中匿名内部类详解
作者:wbb 时间:2022-10-06 14:56:56
java匿名内部类:
1:匿名内部类,匿名内部类也就是没有名字的内部类。
2:匿名内部类的作用
正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写。
3:匿名内部类的实现
匿名内部类的两种实现方式:第一种,继承一个类,重写其方法;第二种,实现一个接口(可以是多个),实现其方法。
4:匿名内部类的创建
匿名类是不能有名称的类,所以没办法引用它们。必须在创建时,作为new语句的一部分来声明它们。
package com.mianshi.test;
/**
* 类名称:AnonymousInnerClassTest
* 描述: 匿名内部类测试
* 创建人:王秋林
* 创建时间:2017-2-12
*/
public class AnonymousInnerClassTest {
public static void main(String args[]){
AnonymousInnerClassTest test = new AnonymousInnerClassTest();
test.show();
}
//在这个方法中构造了一个匿名内部类
private void show(){
Out anonyInter = new Out(){//获取匿名内部类实例
void show(){//重写父类的方法
System.out.println("this is Anonymous InnerClass showing.");
}
};
anonyInter.show();//调用其方法
}
}
//这是一个已经存在的类,匿名内部类通过重写其方法,将会获得另外的实现
class Out{
void show(){
System.out.println("this is Out showing.");
}
}
5:匿名内部类的基本实现
(1)抽象方法实现
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
(2)接口实现
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接口。
(3)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
(4)Runnable接口的匿名内部类实现
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
二、匿名内部类的注意事项
(1)匿名内部类不能有构造方法。
(2)匿名内部类不能定义任何静态成员、方法和类。
(3)匿名内部类不能是public,protected,private,static。
(4)只能创建匿名内部类的一个实例。
(5)一个匿名内部类一定是在new的后面,用其隐含实现一个接口或实现一个类。
(6)因匿名内部类为局部内部类,所以局部内部类的所有限制都对其生效。
(7)匿名类和内部类中的中的this:有时候,我们会用到一些内部类和匿名类。当在匿名类中用this时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。
来源:http://www.2cto.com/kf/201702/598086.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#读取二进制文件方法分析
浅谈MyBatis中@MapKey的妙用
![](https://img.aspxhome.com/file/2023/7/81887_0s.png)
SpringBoot自定义MessageConverter与内容协商管理器contentNegotiationManager详解
![](https://img.aspxhome.com/file/2023/5/60285_0s.png)
SpringCloud2020整合Nacos-Bootstrap配置不生效的解决
MyBatis动态SQL标签的用法详解
Ubuntu中为Android实现Application Frameworks层增加硬件访问服务
Java 多用户登录限制的实现方法
Java Springboot的目的你知道吗
C# 9 新特性——record的相关总结
![](https://img.aspxhome.com/file/2023/5/116655_0s.png)
Android开发人脸识别登录功能
![](https://img.aspxhome.com/file/2023/7/86757_0s.png)
C#泛型的使用及示例详解
![](https://img.aspxhome.com/file/2023/0/89760_0s.png)
Android使用ListView批量删除item的方法
![](https://img.aspxhome.com/file/2023/7/138547_0s.jpg)
Springboot集成RabbitMQ死信队列的实现
![](https://img.aspxhome.com/file/2023/9/77479_0s.png)
Intellij IDEA 阅读源码的 4 个绝技(必看)
![](https://img.aspxhome.com/file/2023/9/106109_0s.png)
Android 数据库文件存取至储存卡的方法
WPF实现钟表效果
![](https://img.aspxhome.com/file/2023/3/102273_0s.jpg)
Java PDF 添加数字签名的实现方法
![](https://img.aspxhome.com/file/2023/6/106096_0s.png)
使用JSON.toJSONString格式化成json字符串时保留null属性
IntelliJ IDEA中查看文件内所有已声明的方法(类似eclipse的outline)
![](https://img.aspxhome.com/file/2023/7/62147_0s.png)