インデクサー

インデクサーは、そのアクセサーがパラメーターを取る点を除き、プロパティと似ています。インデクサーを使用すると、配列と同様にクラスまたは値型のインスタンスに索引を付けることができます。

indexer-specification

indexer-header procedure-division-header access-modifier statement-block access-modifier statement-block

indexer-header

type-specifier indexer-id-signatureメソッドの署名を使用すると、特定の要素のヘッダー内で、渡すパラメーターおよび戻り項目を指定できます。この署名を使用する場合は、メソッド内の手続き部のヘッダーを省きます。 access-modifier attribute-clause

次の例では、クラスが定義されます。このクラスには、値の割り当ておよび取得を行う手段として簡単なゲッターおよびセッター (getter および setter) があります。配列へのアクセスに使用される角かっこでは、0 ベースの索引付けを使用します。丸かっこでは 1 ベースの索引付けを使用します。

       class-id SimpleIndexer.
       01 myArray string occurs 100.

       indexer-id string.
       procedure division using by value i as binary-long.
       getter.
           set property-value to myArray[i]
       setter.
           set myArray[i] to property-value
       end indexer.

       end class.

インデクサー サンプル内の IndexerDemo.cbl も参照してください。このサンプルは、 Start > All Programs > Micro Focus Visual COBOL > Samples , under COBOL for JVM (Windows) or $COBDIR/demo (UNIX). にあります。

明示的なインデクサーの実装

FOR 句を指定すると、このインデクサーは明示的なインターフェイス メンバーの実装になります。このインデクサーを明示的に呼び出すことはできません。このインデクサーは、インターフェイス型にキャストされているこのクラスのインスタンスで対応するインデクサーが呼び出された際に暗黙的に呼び出されます。

(FOR 句による) 明示的なインターフェイスの実装の使用は、クラスが 2 つの異なるインターフェイスを実装しており、これら 2 つのインターフェイスに同じシグネチャを持つメソッドがある場合に特に役立ちます。この場合、FOR 指定を使用すると、2 つの異なるインターフェイス用にメソッドの 2 つの異なる実装を提供できます。

次に例を示します。

interface-id. "Interface1".
indexer-id string.
procedure division using i as binary-long.
end indexer.
end interface.

interface-id. "Interface2". 
indexer-id string.
procedure division using i as binary-long.
end indexer.
end interface.

class-id TwoIndexers implements type Interface1 type Interface2.

indexer-id string for type Interface1.
...
end indexer.

indexer-id string for type Interface2.
...
end indexer.
end class.