Java 简明教程

Java - Pattern Matching with instanceof Operator

从 Java 14 开始,instanceof 运算符引入类型测试模式作为预览功能。类型测试模式具有一个谓词,可使用单个绑定变量指定类型。从 Java 17 开始,它是 Java 的一项标准功能。

Syntax with Enhanced instanceof operator

在下面的代码片段中,我们使用 instanceof 运算符来测试 person 对象是否为 Employee,并将 person 对象赋给 Employee 引用 e,然后使用它来对 Employee 对象执行操作。

if (person instanceof Employee e) {
   return e.getEmployeeId();
}

在增强功能之前,开发人员必须按如下所示强制转换对象类型:

Syntax without Enhanced instanceof operator

在下面的代码片段中,我们将展示使用 Employee 类测试 person 对象的常规方法,然后在 if 块中,我们将 person 强制转换为 Employee e 以对 Employee 对象执行操作。

if (person instanceof Employee) {
   // Unnecessary casting
   Employee e = (Employee)person;
   return e.getEmployeeId();
}

Example - Old Syntax

在此示例中,我们定义了 Person、Employee 和 Manager 类。Employee 和 Manager 扩展 Person 类。在 APITester 类中,我们定义了一个 getId() 方法,该方法将 Person 作为输入,并使用 instanceof 运算符,我们将对象类型测试为 Employee 或 Manager,然后根据 if 块的结果,我们将对象强制转换为 Employee 或 Manager 并据此返回 employeeId 或 managerId。

package com.tutorialspoint;

public class APITester {
   public static void main(String[] args) {
      // Create a Manager Instance
      Person manager = new Manager(23, "Robert");
      // Get and print Id of the manager
      System.out.println(getId(manager));
   }
   // using instanceof operator
   // to test type of Person to be Employee or Manager
   public static int getId(Person person) {
      // If person is Employee, assign it to e
      // in next statement
      if (person instanceof Employee) {
         // Unnecessary typecasting
         Employee e = (Employee)person;
         return e.getEmployeeId();
      }
      // If person is Manager, assign it to m
      // in same statement
      else if (person instanceof Manager) {
         // Unnecessary typecasting
         Manager m = (Manager)person;
         return m.getManagerId();
      }
      return -1;
   }
}
abstract sealed class Person permits Employee, Manager {
   String name;
   String getName() {
      return name;
   }
}
final class Employee extends Person {
   String name;
   int id;
   Employee(int id, String name){
      this.id = id;
      this.name = name;
   }
   int getEmployeeId() {
      return id;
   }
}
non-sealed class Manager extends Person {
   int id;
   Manager(int id, String name){
      this.id = id;
      this.name = name;
   }
   int getManagerId() {
      return id;
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

23

Example - New Syntax

在此示例中,我们定义了 Person、Employee 和 Manager 类。Employee 和 Manager 扩展 Person 类。在 APITester 类中,我们定义了一个 getId() 方法,该方法将 Person 作为输入,并使用 instanceof 运算符,我们将对象类型测试为 Employee 或 Manager,然后在同一个 if 块中,我们将对象分配给 Employee 或 Manager,而不进行强制转换,并根据此返回 employeeId 或 managerId。

package com.tutorialspoint;

public class APITester {
   public static void main(String[] args) {
      // Create a Manager Instance
      Person manager = new Manager(23, "Robert");
      // Get and print Id of the manager
      System.out.println(getId(manager));
   }
   // using instanceof operator
   // to test type of Person to be Employee or Manager
   public static int getId(Person person) {
      // If person is Employee, assign it to e
      // in same statement
      if (person instanceof Employee e) {
         return e.getEmployeeId();
      }
      // If person is Manager, assign it to m
      // in same statement
      else if (person instanceof Manager m) {
         return m.getManagerId();
      }
      return -1;
   }
}
abstract sealed class Person permits Employee, Manager {
   String name;
   String getName() {
      return name;
   }
}
final class Employee extends Person {
   String name;
   int id;
   Employee(int id, String name){
      this.id = id;
      this.name = name;
   }
   int getEmployeeId() {
      return id;
   }
}
non-sealed class Manager extends Person {
   int id;
   Manager(int id, String name){
      this.id = id;
      this.name = name;
   }
   int getManagerId() {
      return id;
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

23