Basic Pascal Tutorial/Chapter 5/Enumerated types/ja

From Free Pascal wiki
(Redirected from Enumerated types/ja)
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

5A - データ型 (著者: Tao Yue, 状態: 原文のまま変更なし)

自分のオリジナルなデータ型を宣言することができる。これはプログラムの型セクション(type section)で行う。

type
 datatypeidentifier = typespecification;

ひとつの方法は列挙型(enumerated type)を作ることである。列挙型の指定は次のようにする。

(identifier1, identifier2, ... identifiern)

例えば、月を宣言したいなら、次のようになる。

type
  MonthType = (January, February, March, April,
              May, June, July, August, September,
              October, November, December);

そうすれば、変数を宣言できるようになる。

var
  Month : MonthType;

変数に列挙値を割り当てることができる。

Month := January;

列挙型にはすべての順序関数が適用できる。ord(January) = 0ord(December) = 11である。

しかし、制限もある。列挙型はプログラムに内在的でテキストファイルから読み込んだり、書き出したりすることはできない。データを読み込み、列挙型に変換しなくてはならない。また、この型で用いられた識別子(例えば、January)は別な型では使用できない。

列挙型の目的のひとつはプログラマーがデータに対して意味のある名前を与えることを可能にすることである。加えて、列挙型は関数や手続きが妥当なパラメータであることを保証してくれる。なぜなら、列挙型の変数だけが許され、列挙値の一つだけを持っているからである。

previous contents next