zoukankan      html  css  js  c++  java
  • java中的匿名内部类总结

    没有名字的内部类
    所以匿名内部类只能使用一次,它通常用来简化代码编写
    前提条件:必须继承一个父类或实现一个接口
     

    实例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
     
     
  • 相关阅读:
    VUE 腾讯云 web端上传视频SDK 上传进度无法显示
    SQL Server 连接数据库报错 (ObjectExplorer)
    Docker 下,搭建 SonarQube 环境 (数据库为 postgres)
    静态代码扫描工具
    windows10 中为文件添加让自己可以使用查看、修改、运行的权限
    mysql8.0.21下载安装详细教程,mysql安装教程
    C# 关于构造函数引证次序讨教
    c# winfrom 读取app.config 自定义节点
    如何在C#中将项目添加到列表中
    如何在C#实现日志功能
  • 原文地址:https://www.cnblogs.com/zsqfightyourway/p/7065005.html
Copyright © 2011-2022 走看看