interface ステートメント

インターフェイスの名前と、インターフェイスを構成するプロパティおよびイベントを宣言します。

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

引数

  • modifiers
    省略可能です。 プロパティの参照可能範囲と動作を制御する修飾子。

  • interfacename
    必ず指定します。 interface の名前。標準的な変数の名前付け規則に従って名前を付けます。

  • implements
    省略可能です。 名前付きのインターフェイスが、定義済みのインターフェイスを実装するか、定義済みのインターフェイスにメンバーを追加するかどうかを示すキーワード。 このキーワードが指定されていない場合、標準の JScript 基本インターフェイスが作成されます。

  • baseinterfaces
    省略可能です。 interfacename で実装されるインターフェイス名のコンマ区切りのリスト。

  • interfacemembers
    省略できます。 interfacemembers には、省略できます。function ステートメントで定義されるメソッドの宣言、または function get ステートメントと function set ステートメントで定義されるプロパティの宣言を指定できます。

解説

JScript の interface 宣言の構文は、class 宣言の構文と似ています。 インターフェイスは、クラスと同様にすべてのメンバーが abstract であり、関数本体を持たないプロパティの宣言とメソッドの宣言だけを含むことができます。 interface ステートメントは、フィールドの宣言、初期化子の宣言、または入れ子になったクラスの宣言を含むことができません。 interface は、implements キーワードを使用して 1 つ以上の interface を実装できます。

class ステートメントは 1 つの基本クラスしか拡張できませんが、複数の interfaces を実装することはできます。 class で複数の interface を実装することにより、C++ などの他のオブジェクト指向言語よりも簡単に多重継承を作成できます。

使用例

次のコードは、1 つの実装がどのようにして複数の interface で継承されるかを示しています。

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 ステートメント

その他の技術情報

修飾子