Rexx-object-oriented

提供:Dev Guides
移動先:案内検索

Rexx-オブジェクト指向

環境の章に従ってooRexxをインストールすると、クラスとオブジェクトを操作することもできます。 以下のコードはすべて、ooRexxインタープリターで実行する必要があることに注意してください。 通常のRexxインタープリターは、このオブジェクト指向コードを実行できません。

クラスとメソッドの宣言

クラスは、次の構文宣言で定義されます。

構文

::class classname
*classname* はクラスに付けられた名前です。

クラスのメソッドは、次の構文宣言で定義されます。

構文

::method methodname

ここで、 methodname はメソッドに付けられた名前です。

クラスのプロパティは、以下の構文宣言で定義されます。

構文

::attribute propertyname
*propertyname* は、プロパティに付けられた名前です。

以下は、Rexxのクラスの例です。

::class student
::attribute StudentID
::attribute StudentName

上記のプログラムについて、以下の点に注意する必要があります。

  • クラスの名前は学生です。
  • クラスには、StudentIDとStudentNameの2つのプロパティがあります。

ゲッターおよびセッターメソッド

GetterメソッドとSetterメソッドは、プロパティの値を自動的に設定および取得するために使用されます。 Rexxでは、属性キーワードを使用してプロパティを宣言すると、getterメソッドとsetterメソッドが既に配置されています。

::class student
::attribute StudentID
::attribute StudentName

上記の例では、StudentIdおよびStudentNameにGetterおよびSetterメソッドがあります。

それらの使用方法の例を、次のプログラムに示します。

/*Main program*/
value = .student~new
value~StudentID = 1
value~StudentName = 'Joe'
say value~StudentID
say value~StudentName

exit 0
::class student
::attribute StudentID
::attribute StudentName

上記のプログラムの出力は次のようになります。

1
Joe

インスタンスメソッド

  • 〜new演算子*を介して、クラスからオブジェクトを作成できます。 クラスのメソッドは、次の方法で呼び出すことができます。
Object~methodname
*methodname* は、クラスから呼び出す必要のあるメソッドです。

次の例は、クラスからオブジェクトを作成する方法と、それぞれのメソッドを呼び出す方法を示しています。

/*Main program*/
value = .student~new
value~StudentID = 1
value~StudentName = 'Joe'
value~Marks1 = 10
value~Marks2 = 20
value~Marks3 = 30
total = value~Total(value~Marks1,value~Marks2,value~Marks3)
say total

exit 0
::class student
::attribute StudentID
::attribute StudentName
::attribute Marks1
::attribute Marks2
::attribute Marks3
::method 'Total'
use arg a,b,c
return (ABS(a) + ABS(b) + ABS(c))

上記のプログラムの出力は次のようになります。

60

複数のオブジェクトを作成する

クラスの複数のオブジェクトを作成することもできます。 次の例は、これを実現する方法を示しています。

ここでは、3つのオブジェクト(st、st1、st2)を作成し、それに応じてインスタンスメンバーとインスタンスメソッドを呼び出しています。

複数のオブジェクトを作成する方法の例を見てみましょう。

/*Main program*/
st = .student~new
st~StudentID = 1
st~StudentName = 'Joe'
st~Marks1 = 10
st~Marks2 = 20
st~Marks3 = 30
total = st~Total(st~Marks1,st~Marks2,st~Marks3)
say total

st1  =  .student~new
st1~StudentID = 2
st1~StudentName = 'John'
st1~Marks1 = 10
st1~Marks2 = 20
st1~Marks3 = 40
total = st1~Total(st1~Marks1,st1~Marks2,st1~Marks3)
say total

st2  =  .student~new
st2~StudentID = 3
st2~StudentName = 'Mark'
st2~Marks1 = 10
st2~Marks2 = 20
st2~Marks3 = 30
total = st2~Total(st2~Marks1,st2~Marks2,st2~Marks3)
say total

exit 0
::class student
::attribute StudentID
::attribute StudentName
::attribute Marks1
::attribute Marks2
::attribute Marks3
::method 'Total'
use arg a,b,c
return (ABS(a) + ABS(b) + ABS(c))

上記のプログラムの出力は次のようになります。

60
70
60

継承

継承は、あるクラスが別のクラスのプロパティ(メソッドとフィールド)を取得するプロセスとして定義できます。 継承を使用すると、情報は階層順に管理可能になります。

他のプロパティを継承するクラスは*サブクラス*(派生クラス、子クラス)と呼ばれ、プロパティが継承されるクラスは*スーパークラス*(ベースクラス、親クラス)と呼ばれます。

Rexxの継承の例を見てみましょう。 次の例では、 Person というクラスを作成しています。 そこから、サブクラスキーワードを使用して、* Studentクラス*を Person のサブクラスとして作成します。

/*Main program*/
st = .student~new
st~StudentID = 1
st~StudentName = 'Joe'
st~Marks1 = 10
st~Marks2 = 20
st~Marks3 = 30
say st~Total(st~Marks1,st~Marks2,st~Marks3)

exit 0
::class Person
::class student subclass Person
::attribute StudentID
::attribute StudentName
::attribute Marks1
::attribute Marks2
::attribute Marks3
::method 'Total'
use arg a,b,c
return (ABS(a) + ABS(b) + ABS(c))

上記のプログラムの出力は次のようになります。

60