Java Class isInterface() 方法



描述

Java Class isInterface() 方法用于确定指定的 Class 对象是否表示接口类型。

声明

以下是 java.lang.Class.isInterface() 方法的声明

public boolean isInterface()

参数

返回值

如果此对象表示接口,则此方法返回 true,否则返回 false。

异常

获取类的接口状态示例

以下示例演示了 java.lang.Class.isInterface() 方法的用法。在此程序中,我们创建了一个 ClassDemo 的实例,然后使用 getClass() 方法获取实例的类。使用 isInterface(),我们获取了接口状态并打印出来。

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String[] args) {

      ClassDemo c = new ClassDemo();
      Class cls = c.getClass();

      // determines if the specified Class object represents an interface type
      boolean retval = cls.isInterface();
      System.out.println("It is an interface ? " + retval);       
   }
}

输出

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

It is an interface ? false

获取接口的接口状态示例

以下示例演示了 java.lang.Class.isInterface() 方法的用法。在此程序中,我们使用了 FunctionalInterface 的类。使用 isInterface(),我们获取了接口状态并打印出来。

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String[] args) {
      Class cls = FunctionalInterface.class;
      
	  // determines if the specified Class object represents an interface type
      boolean retval = cls.isInterface();
      
	  System.out.println("It is an interface ? " + retval);       
   }
}

输出

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

It is an interface ? true

获取 List 的接口状态示例

以下示例演示了 java.lang.Class.isInterface() 方法的用法。在此程序中,我们使用了 List 的类。使用 isInterface(),我们获取了接口状态并打印出来。

package com.tutorialspoint;

import java.util.List;

public class ClassDemo {

   public static void main(String[] args) {
      Class cls = List.class;
      
	  // determines if the specified Class object represents an interface type
      boolean retval = cls.isInterface();
      
	  System.out.println("It is an interface ? " + retval);       
   }
}

输出

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

It is an interface ? true
java_lang_class.htm
广告

© . All rights reserved.