zoukankan      html  css  js  c++  java
  • 匿名内部类

    • 内部类是指在一个外部类的内部再定义一个类。
    • 匿名内部类也就是没有名字的内部类
    • 正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写。
    • 使用匿名内部类的前提条件是必须继承一个父类或实现一个接口

    继承抽象类的匿名内部类

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


    ------------------------------------------------------------------------------------------------------------------------------------------------------------
    实现接口的匿名内部类

     1 interface Person {
     2     public void eat();
     3 }
     4 public class Demo {
     5     public static void main(String[] args) {
     6         Person p = new Person() {
     7             public void eat() {
     8                 System.out.println("eat something");
     9             }
    10         };
    11         p.eat();
    12     }
    13 }
    • 匿名内部类不能有构造方法
    • 匿名内部类不能定义任何静态成员、方法和类
    • 匿名内部类不能是public、protected、private、static
    • 只能创建匿名内部类的一个实例
    • 一个匿名内部类一定是在new的后面,用其隐含实现一个接口或实现一个类;
    • 因匿名内部类为局部内部类,所以局部内部类的所有限制都对其生效;
    • 在匿名类中用this时,这个this指的是匿名类本身。如果我们要使用外部类的方法和变量的话,应该加上外部类的类名。
    • 匿名内部类常用在多线程的实现上,因为要实现多线程必须继承Thread类或是实现Runnable接口。

    Thread类的匿名内部类实现

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

    等价的非匿名内部类实现

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

    Runnable接口的匿名内部类实现

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

    等价的非匿名内部类实现

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

     

     

     

  • 相关阅读:
    000-ESP32学习开发(SDK)-ESP32开发板使用说明
    2-STM32 替换说明-CKS32, HK32, MM32, APM32, CH32, GD32, BLM32, AT32(推荐), N32, HC华大系列
    002-CH579M学习开发-官方资料学习说明,开发板蓝牙(蓝牙定位),网口通信测试
    001-CH579M学习开发-硬件使用说明,下载和运行第一个程序
    STM32+CH395Q(以太网)基本控制篇(自建物联网平台)-硬件使用说明
    17-网络芯片CH395Q学习开发-片内EEPROM读写实验
    16-网络芯片CH395Q学习开发-低功耗实验
    15-网络芯片CH395Q学习开发-DNS 域名解析
    14-1-网络芯片CH395Q学习开发-WEB服务器-网页到底是啥, web服务器是啥, 网页如何显示的显示图片和视频
    13-网络芯片CH395Q学习开发-模块使用Socket0作为MAC RAW
  • 原文地址:https://www.cnblogs.com/-maji/p/7281499.html
Copyright © 2011-2022 走看看