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

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

hot3.png

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

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

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

 

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

转载于:https://my.oschina.net/daniels/blog/708129

你可能感兴趣的文章
NPM酷库:winston 多路日志记录
查看>>
vue中的methods、computed和watch
查看>>
js 中的基本类型,引用类型,基本包装类型
查看>>
Word强大插件Writage使用手册
查看>>
CentOS7下PHP7.2安装redis扩展
查看>>
聊聊FilterSecurityInterceptor
查看>>
让数据库变快的10个建议
查看>>
Spring Boot 揭秘与实战
查看>>
【327天】跃迁之路——程序员高效学习方法论探索系列(实验阶段85-2017.12.29)...
查看>>
Redux 源码解析系列 -- Redux的实现思想
查看>>
数人云实录|微服务企业级落地将会带来哪些转变?
查看>>
[译]Flask教程--静态文件
查看>>
Laravel 大将之 View 模块
查看>>
Microsoft Graph:连接每个应用都需要的基础数据
查看>>
研究人员发现GPU侧道攻击漏洞
查看>>
解读 2018之Go语言篇(上):为什么Go语言越来越热?
查看>>
webpack 2 实践系列(一) — 安装与入门
查看>>
力荐!这些工具可以帮你写出干净的代码
查看>>
Chuck Cobb谈敏捷组织中PMO的角色
查看>>
Microsoft开源Visual Studio Test
查看>>