java中的匿名内部类总结

news/2024/7/5 3:14:50

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

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

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

 

实例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


http://lihuaxi.xjx100.cn/news/242856.html

相关文章

Windows10下SSH远程拷贝文件

因为今天需要把服务器上面的东西备份一下&#xff0c;自己平时也在windows下面做的测试&#xff0c;所以用windows在服务器拷贝文件到本地。 首先需要下载一个工具pscp.exe 下载链接 然后再把它移动到这个目录下面就行了 WINR打开命令行 pscp -r 用户名ip:/root/flask E:/refl…

零基础小白java培训学习指南

java程序猿在互联网行业一直都被大家认为是高薪的职业&#xff0c;很多人都想要学习java技术&#xff0c;不管是通过自学还是报班学习&#xff0c;都需要有学习计划的&#xff0c; 下面小编就为大家分享一下零基础小白java培训学习指南&#xff0c;希望可以帮助到大家。 零基础…

The system cannot find the file specified

在家工作&#xff0c;程序在家里的电脑运行时&#xff0c;出现一个异常&#xff0c;还是第一见到&#xff1a; Server Error in / Application. The system cannot find the file specified Description: An unhandled exception occurred during the execution of the current…

LeetCode Python题解(一)----双指针法

根据&#xff1a; github优秀创作者. 算法思想 1.双指针法 2.排序 3.贪心思想 4.二分查找 5.分冶 6.搜索 7.动态规划 8.数学 1. 双指针法&#xff1a; 双指针主要用于遍历数组&#xff0c;两个指针指向不同的元素&#xff0c;从而协同完成任务。 1.1 有序数组的 Two Sum 题…

推荐几款chrome上比较好用的书签收藏夹插件

今天有个人问我chrome浏览器器上有没有可以稍后阅读的插件啊&#xff1f;她其实想问的就是书签收藏夹插件&#xff0c;因为我们在互联网上一不小心就会看到很多感兴趣的内容&#xff0c;但是时间有限暂时无法阅读&#xff0c;以后保存下来有时间的时候再看。我相信这是很多人共…

0基础学怎么学习python

​ Python相对于其他编程语言来说是比较简单的&#xff0c;非常适合零基础的小白学习&#xff0c;想要进入到互联网行业&#xff0c;可以优先选择学习Python&#xff0c;那么下面小编就来为大家详细的介绍一下0基础学怎么学习python? ​  0基础学怎么学习python? 1、要读书…

Linux中的文件复制:cp和scp

在使用操作系统的使用过程中&#xff0c;常常需要复制文件到本地或者传输文件到其他电脑上&#xff0c;这时候用到两个命令cp和scp。cp命令用来复制文件或者目录。scp是secure copy的简写&#xff0c;用来在Linux下进行加密的远程传输文件或者目录。cp和scp是Linux中功能强大且…

bat批处理文件启动Eclipse和ivy本地仓库的配置

一、bat批处理文件启动Eclipse 所需文件&#xff1a; 1、eclipse 2、jre 3、startup-eclipse.bat 确保以上三个文件夹同级 startup-eclipse.bat: set dir%CD% cd %dir%\eclipse eclipse.exe -vm %dir%\jre\bin -vmargs -Xms512M -Xmx1024M -XX:PermSize128M -XX:MaxPermSize256…