そしてCOBOLerへ…

仕事でCOBOLを扱うことになった。少し勉強中。とりあえずオブジェクト指向してみる。

まず、speakメソッドを有するAnimalインタフェースを定義する。

      *>「Animal」インタフェース
       identification division.
       interface-id. Animal.

       procedure division.
         identification division.
         method-id. speak.
         procedure division.
         end method speak.
       end interface Animal.

で、それを実装するのがDogクラスとDuckクラス。

     *>「Dog」クラス
       identification division.
       class-id. Dog inherits Base.

       environment division.
       configuration section.
       repository.
                   class Base.
                   interface Animal.
         identification division.
         object. implements Animal.
         procedure division.

           identification division.
           method-id. speak.
           procedure division.
             display 'bowwow by Dog'.
           end method speak.

         end object.
       end class Dog.

      *>「Duck」クラス
       identification division.
       class-id. Duck inherits Base.

       environment division.
       configuration section.
       repository.
                   class Base.
                   interface Animal.
         identification division.
         object. implements Animal.
         procedure division.

           identification division.
           method-id. speak.
           procedure division.
             display 'quack by Duck'.
           end method speak.

         end object.
       end class Duck.

最後にDogクラスとDuckクラスのオブジェクトに対してspeakメソッドを呼び出す。

      *>メイン
       identification division.
       program-id. hoge.

       environment division.
       configuration section.
       repository.
                   class Dog.
                   class Duck.
                   interface Animal.
       data division.
       working-section.
       01 obj usage object reference Animal.

       procedure division.
         invoke Dog 'NEW' returning obj.
         invoke obj speak.

         invoke Duck 'NEW' returning obj.
         invoke obj speak.

         stop run.
       end program hoge.

こうなるはず。*1

bowwow by Dog
quack by Duck

ごちゃごちゃしてはる。。


*1:フリーでオブジェクト指向に対応したCOBOL処理系を見つけることができなかった。。。