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 }

     

     

     

  • 相关阅读:
    Java堆栈内存总结
    Java String使用总结
    Java异常处理
    音乐之声——midi制作原理
    Windows使用问题总结
    Chrome浏览器的使用
    搭建svn服务器
    Windows实用快捷键
    Phone文件备份
    9大背包第一弹 | 01背包
  • 原文地址:https://www.cnblogs.com/-maji/p/7281499.html
Copyright © 2011-2022 走看看