: メソッド探索 (Method Lookup)
: クラスベース言語
: クラスベース言語
以下の例を紹介する。
class cell is
var contents: Integer:=0;
method get(): Integer is
return self. contents;
end
method set(n: Integer) is
self. contents:=n;
end
end;
- self はホストオブジェクトを指す。
- オブジェクトは、内部的には attribute レコードとして表現される。
- 図2.1 で storage model を示す。
- オブジェクトは new c で生成される。正確には、
クラス c の attribute レコードを割り当て、それへの参照を返す。
- new c により生成されるものは、「クラス c の
オブジェクト」または、「クラス c のインスタンス」という。
var myCell: InstanceTypeOf(cell):=
new cell
- タイプ InstanceTypeOf(cell) を導入する。
タイプとクラスは明確に区別する。
InstanceTypeOf(cell) の代わりに、 cell (クラス名そのもの)
を用いることも可能であるが、そのようなそのようなとクラスの同一は
問題を引き起こす。
- タイプ myCell を使って、 contents フィールドを extract
することも可能である。また、以下のように update することも可能である。
- 一つの cell を使って、メソッド呼び出しをすることも可能である。
例えば myCell.set(3) などである。
- 手続き ( procedure) を使って以下のような、メソッド呼び出しをする
ことも可能である。
procedure double(aCell: InstanceTypeOf(cell)) is
aCell.set(2 * aCell.get());
end;
平成12年8月22日