Rational Developer for System z
COBOL for Windows バージョン 7.5 プログラミング・ガイド


例: メソッドの定義

次の例は、直前の例に、Account クラスのインスタンス・メソッド定義を 追加し、Java Check クラスの定義を示します。

(直前の例は 例: クラスの定義でした。)

Account クラス

 cbl thread,pgmname(longmixed)
 Identification Division.
 Class-id. Account inherits Base.
 Environment Division.
 Configuration section.
 Repository.
     Class Base    is “java.lang.Object”
     Class Account is “Account”.
*
*  (FACTORY paragraph not shown)
*
 Identification division.
 Object.
  Data division.
  Working-storage section.
  01 AccountNumber  pic 9(6).
  01 AccountBalance pic S9(9) value zero.
*
  Procedure Division.
*
*    init method to initialize the account:
   Identification Division.
   Method-id. “init”.
   Data division.
   Linkage section.
   01 inAccountNumber pic S9(9) binary.
   Procedure Division using by value inAccountNumber.
     Move inAccountNumber to AccountNumber.
   End method “init”.
*
*    getBalance method to return the account balance:
   Identification Division.
   Method-id. “getBalance”.
   Data division.
   Linkage section.
   01 outBalance pic S9(9) binary.
   Procedure Division returning outBalance.
     Move AccountBalance to outBalance.
   End method “getBalance”.
*
*    credit method to deposit to the account: 
   Identification Division.
   Method-id. “credit”.
   Data division.
   Linkage section.
   01 inCredit   pic S9(9) binary.
   Procedure Division using by value inCredit.
     Add inCredit to AccountBalance.
   End method “credit”.
*
*    debit method to withdraw from the account: 
   Identification Division.
   Method-id. “debit”.
   Data division.
   Linkage section.
   01 inDebit    pic S9(9) binary.
   Procedure Division using by value inDebit.
     Subtract inDebit from AccountBalance.
   End method “debit”.
*
*    print method to display formatted account number and balance:
   Identification Division.
   Method-id. “print”.
   Data division.
   Local-storage section.
   01 PrintableAccountNumber  pic ZZZZZZ999999.
   01 PrintableAccountBalance pic $$$$,$$$,$$9CR.
   Procedure Division.
     Move AccountNumber  to PrintableAccountNumber
     Move AccountBalance to PrintableAccountBalance
     Display “ Account: ” PrintableAccountNumber
     Display “ Balance: ” PrintableAccountBalance.
   End method “print”.
*
 End Object.
*
 End class Account.

Check クラス

/** 
 * A Java class for check information 
 */
public class Check {
  private CheckingAccount payer;
  private Account         payee;
  private int             amount;

  public Check(CheckingAccount inPayer, Account inPayee, int inAmount) {
    payer=inPayer;
    payee=inPayee;
    amount=inAmount;
  }

  public int getAmount() {
    return amount;
  }

  public Account getPayee() {
    return payee;
  } 
}

関連タスク
オブジェクト指向アプリケーションのコンパイル、リンク、および実行


ご利用条件 | フィードバック

Copyright IBM Corporation 1996, 2008.
このインフォメーション・センターでは Eclipse テクノロジーが採用されています。(http://www.eclipse.org)