Share via


interface 陳述式

更新:2007 年 11 月

宣告介面的名稱,以及組成介面的屬性 (Property) 和方法。

 [modifiers] interface interfacename [implements baseinterfaces] {    [interfacemembers] }

引數

  • 修飾詞
    選擇項。修飾詞,控制屬性的可視性和行為。

  • interfacename
    必要項。interface 的名稱,後面跟著標準變數命名規範。

  • implements
    選擇項。關鍵字,表示具名介面實作先前定義的介面,或將成員加入至先前定義的介面中。如果沒有使用這個關鍵字,會建立標準的 JScript 基底介面。

  • baseinterfaces
    選擇項。逗號分隔的介面名稱清單,由 interfacename 實作。

  • interfacemembers
    選擇項。interfacemembers 可以是方法宣告 (以 function 陳述式 (Statement) 定義) 或屬性宣告 (以 function get 陳述式和 function set 陳述式定義)。

備註

在 JScript 中,interface 宣告的語法與 class 宣告的語法相似。介面是就像是一個類別,其每個成員都是抽象的;它只能包含屬性和不具函式主體 (Function Body) 的方法宣告。interface 可能不包含欄位宣告、初始設定式宣告或巢狀類別 (Nested Class) 宣告。interface 可以使用 implements 關鍵字來實作一或多個介面

一個類別只能擴充一個基底類別,但是一個類別可以實作多個介面。這種由類別實作的多個介面允許使用比在其他物件導向語言中 (例如 C++ 中) 還要簡單的多重繼承形式。

範例

下列程式碼說明一個實作如何由多重介面繼承。

interface IFormA {
   function displayName();
}

// Interface IFormB shares a member name with IFormA.
interface IFormB {
   function displayName();
}

// Class CForm implements both interfaces, but only one implementation of
// the method displayName is given, so it is shared by both interfaces and
// the class itself.
class CForm implements IFormA, IFormB {
   function displayName() {
      print("This the form name.");
   }
}

// Three variables with different data types, all referencing the same class.
var c : CForm = new CForm();
var a : IFormA = c;
var b : IFormB = c;

// These do exactly the same thing.
a.displayName();
b.displayName();
c.displayName();

本程式的輸出為:

This the form name.
This the form name.
This the form name.

需求

.NET 版本

請參閱

參考

class 陳述式

function 陳述式

function get 陳述式

function set 陳述式

其他資源

修飾詞