次の例は、Account クラスの CheckingAccount サブクラスのインスタンス・メソッド定義を示しています。
processCheck メソッドは、Check クラスの Java インスタンス・メソッド getAmount および getPayee を呼び出して、チェック・データを取得します。Account クラスから継承した credit および debit インスタンス・メソッドを呼び出して、当座の受取人を貸し方に記入し、支払人を借方に記入します。
print メソッドは、Account クラスに定義されている print インスタンス・メソッドを オーバーライドします。オーバーライドした print メソッドを呼び出して、口座状況を表示し、また当座手数料も表示します。CheckFee は、サブクラスに定義するインスタンス・データ項目です。
(Account クラスは、例: メソッドの定義で示しました。)
cbl thread,pgmname(longmixed)
Identification Division.
Class-id. CheckingAccount inherits Account.
Environment Division.
Configuration section.
Repository.
Class CheckingAccount is “CheckingAccount”
Class Check is “Check”
Class Account is “Account”.
*
* (FACTORY paragraph not shown)
*
Identification division.
Object.
Data division.
Working-storage section.
01 CheckFee pic S9(9) value 1.
Procedure Division.
*
* processCheck method to get the check amount and payee,
* add the check fee, and invoke inherited methods debit
* to debit the payer and credit to credit the payee:
Identification Division.
Method-id. “processCheck”.
Data division.
Local-storage section.
01 amount pic S9(9) binary.
01 payee usage object reference Account.
Linkage section.
01 aCheck usage object reference Check.
*
Procedure Division using by value aCheck.
Invoke aCheck “getAmount” returning amount
Invoke aCheck “getPayee” returning payee
Invoke payee “credit” using by value amount
Add checkFee to amount
Invoke self “debit” using by value amount.
End method “processCheck”.
*
* print method override to display account status:
Identification Division.
Method-id. “print”.
Data division.
Local-storage section.
01 printableFee pic $$,$$$,$$9.
Procedure Division.
Invoke super “print”
Move CheckFee to printableFee
Display “ Check fee: ” printableFee.
End method “print”.
*
End Object.
*
End class CheckingAccount.
関連タスク
オブジェクト指向アプリケーションのコンパイル、リンク、および実行
メソッドの呼び出し (INVOKE)
インスタンス・メソッドのオーバーライド
オーバーライドされたスーパークラス・メソッドの呼び出し