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
廣告