Java匿名内部类的完整指南

1,280 阅读3分钟

Java匿名内部类 是一个 没有名字的非活动类 (非静态嵌套类)。它在Java中被用来覆盖接口和类的方法。与其他类不同,它没有任何构造函数(因为构造函数的名称和类的名称必须相同,但匿名内类没有名称)。它既可以用一个接口来创建,也可以用一个类来创建。

Java匿名内层类

Java中的匿名内类只能扩展一个类或只实现一个接口,而不像其他类可以同时扩展一个类和实现几个接口。因此,一个匿名内类将只创建一个对象。匿名内类的表达式包括:

  1. 将被实现的接口名称,或者将被扩展的类名称
  2. 操作符
  3. 包围类的构造函数的参数的括号
  4. 主体包含方法声明

匿名内类可以访问包围类的成员。它也可以访问包围类中的局部变量,只要它们被声明为 final

它可以包含作为常量的静态成员。匿名的内层类可以有方法,但主体内不允许有语句。

当一个嵌套类被编译时,系统中会创建两个类文件,名称分别为 OuterClass.classOuterclassNestedClass.class。此外,匿名的内部类被命名为OuterClassNestedClass.class*。此外,匿名的内部类被命名为 *OuterClass1.class

不同种类的嵌套类

请看下图:

Java Anonymous Inner Class

表达式

见下面的语法:

OuterClass objectname = new OuterClass() 
{     
      //method declarations and data members
};

请看下面的程序:

我们也可以有匿名的内层类来实现接口。下面的程序显示了使用接口实现匿名内类的过程:

//interface containing two abstract methods
interface Animals {
  public void showPets();
  public void showWild();
}

public class Example1 {
  public static void main(String[] args) {
    // creating anonymous inner class implementing the interface
    Animals animal = new Animals() {
      // body of anonymous inner class
      // implementing the abstract methods of the interface
      public void showPets() {
        System.out.println("Pet Animals Are: DOGS, CATS");
      }

      public void showWild() {
        System.out.println("Wild Animals Are: LION, TIGER, WOLF");
      }
    };

    // calling the methods
    animal.showPets();
    animal.showWild();
  }
}

请看下面的例子:

Anonymous Inner Class

在这个程序中,匿名内层类被用来实现一个名为 Animals的接口 。这个接口有两个抽象的方法,这个匿名类被用来实现这些方法。

请看下面的程序,它显示了用一个具体的类来实现匿名的内部类:

//creating a class with some methods
class Cars {
  void color() {
    System.out.println("color of the car: RED");
  }

  void mileage() {
    System.out.println("mileage of the car: 28 kmpl");
  }
}

public class Example2 {
  public static void main(String[] args) {
    // creating the anonymous class and overridding some methods
    Cars ford = new Cars() {
      void mileage() {
        System.out.println("mileage of the car: 16 kmpl");
      }
    };

    System.out.println("Methods called by Anonymous class object: ");
    ford.color(); // not overridded
    ford.mileage();

    // creating normal object and calling its methods
    System.out.println("\nMethods called by Normal class object: ");
    Cars defaultCar = new Cars();
    defaultCar.color();
    defaultCar.mileage();
  }
}

请看下面的输出:

Java Anonymous Inner Class Tutorial

在这个程序中,匿名内部类被用来覆盖一个名为 Cars的具体类的一些方法 。这个具体的类包含2个方法,这个匿名类被用来覆盖其中一个方法。

请看该程序,它显示了匿名内部类在方法参数内的实现:

//creating an interface with an abstract method
interface Salutation {
  public void morning();
}

// creating a class with method invoking Salutaion's method
class WishAll {
  public void greet(Salutation s) {
    s.morning();
  }
}

public class Example3 {
  public static void main(String[] args) {
    // object for WishAll class
    WishAll wish = new WishAll();

    // creating anonymous class inside the method argument of WishAll class.
    // it will implement the Salutation interface
    wish.greet(new Salutation() {
      public void morning() {
        System.out.println("Wishing all a very good morning!");
      }
    });
  }
}

请看下面的输出:

Anonymous Inner Class Tutorial With Example

在这个程序中,匿名内部类在 WishAll类的方法参数中被创建 。该方法以接口 Salutation的一个对象 为参数,所以实现这个匿名内类就实现了该接口及其抽象方法。

本教程就到此为止。