Java 简明教程

Java - Anonymous Classes

Java Anonymous Class

Java 中的匿名类是 inner class,它完全在没有任何类名的情况下声明。换句话说,Java 中的不带名称的内部类称为匿名内部类。因为它没有名称,所以它不能有构造函数,因为我们知道构造函数名称与类名称相同。

An anonymous class in Java is an inner class which is declared without any class name at all. In other words, a nameless inner class in Java is called an anonymous inner class. Since it does not have a name, it cannot have a constructor because we know that a constructor name is the same as the class name.

Use of Java Anonymous Inner Classes

inner classes匿名类用于在仅在特定目的下一次性需要一个简单 class 时创建。例如,实现一个 interface 或扩展一个类。

Anonymous inner classes are used when you want to create a simple class that is needed for one time only for a specific purpose. For example, implementing an interface or extending a class.

Defining Anonymous Class in Java

你可以在一步中使用 new 操作符同时定义一个匿名内部类并创建其对象。

You can define an anonymous inner class and create its object using the new operator at the same time in one step.

Syntax

匿名嵌套类的语法如下 −

The syntax of anonymous nested class is as follows −

new(argument-list){
   // Anonymous class body
}

Types of Anonymous Inner Classes in Java

  1. Anonymous inner class that extends a class

  2. Anonymous inner class that implements an interface

  3. Anonymous inner class as an argument

1. Anonymous inner class that extends a class

您可以使用匿名内部类来扩展 Java 中的类。

You can use an anonymous inner class to extend a class in Java.

在以下示例中,我们使用新关键字来创建具有父类类型的匿名内部类的对象。

In the following example,we’re using a new keyword to create an object of the anonymous inner class that has a reference of parent class type.

package com.tutorialspoint;

class Car {
   public void engineType() {
      System.out.println("Turbo Engine");
   }
}
public class Tester {
   public static void main(String args[]) {
      Car c1 = new Car();
      c1.engineType();
      Car c2 = new Car() {
         @Override
         public void engineType() {
            System.out.println("V2 Engine");
         }
      };
      c2.engineType();
   }
}

Output

如果您编译并执行上述程序,将得到以下结果 −

If you compile and execute the above program, you will get the following result −

Turbo Engine
V2 Engine

2. Anonymous inner class that implements an interface

您可以在 Java 中使用匿名内部类来实现接口。

You can use an anonymous inner class to implement an interface in Java.

在以下示例中,我们使用新关键字来创建具有接口类型的匿名内部类的对象。

In the following example,we’re using a new keyword to create an object of the anonymous inner class that has a reference of an interface type.

package com.tutorialspoint;

interface Software {
   public void develop();
}
public class Tester {
   public static void main(String args[]) {
      Software s = new Software() {
         @Override
         public void develop() {
            System.out.println("Software Developed in Java");
         }
      };
      s.develop();
      System.out.println(s.getClass().getName());
   }
}

Output

如果您编译并执行上述程序,将得到以下结果 −

If you compile and execute the above program, you will get the following result −

Software Developed in Java
com.tutorialspoint.Tester$1

3. Anonymous inner class as an argument

我们可以将匿名内部类用作参数,以便将其传递给方法或构造函数。

We can use the anonymous inner class as an argument so that it can be passed to methods or constructors.

在以下示例中,我们将匿名内部类作为参数传递给一个方法。

In the following example,we’re passing an anonymous inner class as an argument to one method.

package com.tutorialspoint;

abstract class Engine {
   public abstract void engineType();
}
class Vehicle {
   public void transport(Engine e) {
      e.engineType();
   }
}
public class Tester {
   public static void main(String args[]) {
      Vehicle v = new Vehicle();
      v.transport(new Engine() {
         @Override
         public void engineType() {
            System.out.println("Turbo Engine");
         }
      });
   }
}

Output

如果您编译并执行上述程序,将得到以下结果 −

If you compile and execute the above program, you will get the following result −

Turbo Engine