zoukankan      html  css  js  c++  java
  • 匿名内部类的一些看法

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

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

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

    实例1:不使用匿名内部类来实现抽象方法

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

    运行结果:eat something

    可以看到,我们用Child继承了Person类,然后实现了Child的一个实例,将其向上转型为Person类的引用

    但是,如果此处的Child类只使用一次,那么将其编写为独立的一个类岂不是很麻烦?

    这个时候就引入了匿名内部类

    实例2:匿名内部类的基本实现

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

    运行结果:eat something

    可以看到,我们直接将抽象类Person中的方法在大括号中实现了

    这样便可以省略一个类的书写

    并且,匿名内部类还能用于接口上

    实例3:在接口上使用匿名内部类

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

    运行结果:eat something

    由上面的例子可以看出,只要一个类是抽象的或是一个接口,那么其子类中的方法都可以使用匿名内部类来实现

    最常用的情况就是在多线程的实现上,因为要实现多线程必须继承Thread类或是继承Runnable接口

    实例4:Thread类的匿名内部类实现

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

    运行结果:1 2 3 4 5

    实例5:Runnable接口的匿名内部类实现

     1 public class Demo {
     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 t = new Thread(r);
    11         t.start();
    12     }
    13 }

    运行结果:1 2 3 4 5

  • 相关阅读:
    Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
    旋转二维数组
    replace empty char with new string,unsafe method和native implementation的性能比较
    判断一字符串是否可以另一字符串重新排列而成
    移除重复字符的几个算法简单比较
    也来纠结一下字符串翻转
    判断重复字符存在:更有意义一点
    程序员常去网站汇总
    sublime
    针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_ACCESS)
  • 原文地址:https://www.cnblogs.com/zyxiaohuihui/p/4685193.html
Copyright © 2011-2022 走看看