LOTUSSCRIPT 言語


メンバのプロパティとメソッドを定義する
プロパティとメソッドはクラスに結びついていて、そのクラスに属するオブジェクトと一緒にのみ使用できます。プロパティとメソッドは Class ステートメントの中で定義します。
メンバ変数、サブルーチン、関数、およびプロパティを含むクラスの構文

次の Stack クラスは、いくつかのプロパティとメソッドを使用して、スタックデータ構造体に関して単純な push および pop 操作を実行します。

Class Stack
 Private idx As Integer
 Stack List As Variant
 Public stackName As String
 Private Sub CheckStack ' Sub is visible only within
                        ' the class.
   If idx% = 0 Then Error 999
 End Sub

Sub New
   idx% = 0        ' Initialize idx.
 End Sub

  Private Property Set topValue As Variant
    CheckStack
    Stack(idx%) = topValue  ' Set the top value on the stack.
  End Property
 
  Private Property Get topValue As Variant
    CheckStack
    topValue = Stack(idx%)  ' Get the top value on the stack.
  End Property

  ' Same as Get for topValue.
  Function Top
     Top = topValue   ' Call the topValue Get method.
  End Function

  Sub Push(v)         ' Push a value on the stack.
    idx% = idx%+1
    topValue = v
  End Sub

  Function Pop        ' Pop a value off the stack.
    Pop = topValue
    Erase Stack(idx%)
    idx% = idx%-1
  End Function

  ' Read-only property.There is no Set for Count.
  Property Get Count
    Count = idx%       ' Count the values on the stack.
  End Property

End Class

Dim St As New Stack
Call St.Push("An item on the stack")
Call St.Push("Another item on the stack")
Print "# of items on the stack is ";St.Count
Print "TopValue is ";St.Top

Sub New と Sub Delete を宣言する (オブジェクトの初期化と削除)

クラス定義の中で、2 つの特別なサブルーチンを作成できます。2 つとも常に Public です。Private として宣言することはできません。


これらのサブルーチンは、スクリプト内のイベントとして使用できます。たとえば、Sub New を使用してファイルを開き、Sub Delete を使用してファイルを閉じる File クラスを作成できます。同様に、Sub New を使用して新規ページを開始し、テキストを揃え、マージンを設定し、Sub Delete を使用して印刷ジョブを終了させる PrintJob クラスを作成できます。

次のスクリプト内の Sub New は、CustomerAccount オブジェクトのメンバ変数を初期化します。新規 Account オブジェクトを作成する Set ステートメントは、Account クラス用の Sub New で必要とする 3 つのパラメータも渡します。Sub New は、新たに作成されるオブジェクトの 3 つのメンバ変数 (balance@、acctNum&、customerNum$) に引数の値を代入します。

Class Account
 balance As Currency
 acctNum As Long
 customerNum As Long

' Declare Sub New.
 Sub New (newBal As Currency, newAcctNum As Long, _
   newCustNum As Long)
    balance@ = newBal@
    acctNum& = newAcctNum&
    customerNum& = newCustNum&
    Print "New Parms=";balance@, acctNum&, customerNum&
 End Sub

' Declare Sub Delete.
 Sub Delete
    Print "Deleting account record for customer:";customerNum
 End Sub

End Class
'.....
Dim CustomerAccount As Account

' Create the object.
Set customerAccount = New Account(1234.56, 10001991, 5412)          
Delete customerAccount     ' Explicitly delete the object.

関連項目