:
Delegation
:
オブジェクトベース言語
:
Embedding による継承と Delegation による継承
Embedding
継承の一つの可能な解釈は、ホストオブジェクトが donor オブジェクトの attribute の複製を含んでいることである。これを、継承の embedding 解釈 または、単に embedding という(図4-1)。
embedding は「呼び出しの受側」として
self
の標準的意味を 最も簡単に説明することができる。embedding 解釈 において、継承メソッド呼び出しは、ちょうど original メソッド呼び出しを 意味する。
embedding はプロトタイプベース言語において最初の提案の一つであっ た。最近では単に Kevo と Obliq のような言語に適用されているだけである。 このような言語を embedding ベースという。
前節で説明したように、継承は explicitly または implicitly に 区別することができる。embedding 継承の explicit な形は、古いオブジェク トの一部分を新しいオブジェクトに reassembing するものとして考えること ができる。embedding 継承の implicit な形は現存するオブジェクトのクロー ンを新しい attribute と共に拡張する方法である。
embedding の explicit バージョンでは、指定したオブジェクトから、 メソッドとフィールドを複製する。例えば、回復可能
は以下のようになる。
object
cell: Cell
is
var
contents: Integer := 0;
method
get(): Integer
is
return self
.contents
end;
method
set(n: Integer)
is
self
.contents:=n
end;
end;
object
reCellExp:ReCell
is
var
contents: Integer:=cell.contents;
var
backup: Integer:=0
;
method
get(): Integer
is
return embed
cell.get()
;
end;
method
set(n:Integer)
is
self
.backup:=
self
.contents;
embed
cell.set(n);
end;
method
restore()
is
self
.contents:=
self
.backup
end;
end;
embed
と書いてオブジェクト
のメソッドを 現在オブジェクトに embed する。この embedding コンストラクトにより、
メソッドの implicit 継承と
メソッドでの
super
の出現の両方を入れ替えている。
embed
の意味は
の
メソッド (現在の「自身」を意味する
self
を伴った)を実行する。
のコード を
に embeded し、
オブジェクトには影響を与えない。
メソッドのコードはより冗長である。以下のように省略して書く ことができる。
method
get
copied from
cell
しかし
embed
コンストラクトはまだ必要である。
型づけの問題を避けるために、
embed
に続くオブジェクト (そのタイプではなく、オブジェクトそのもの)は、静的に知らなければならな い。この意味において、状況はサブクラス定義(スーパークラスは静的に知ら なければならない)に似ている。
embedding の implicit バージョンでは、新しいオブジェクトに複製さ れるべきメソッドとフィールドの donor としてあるオブジェクトを設定する。 例えば、回復可能な
の例は以下のようになる。
object
cell:Cell
is
var
contents: Integer:=0
;
method
get():Integer
is
return self
.contents
end;
method
set(n: Integer)
is
self
.contents:=n
end;
end;
object
reCellImp: ReCell
extends
cell
is
var
backup: Integer:=0;
override
set(n: Integer)
is
self
.backup:=
self
.contents;
embed
cell.set(n);
end;
method
restore()
is
self
.contents:=
self
.backup
end;
end;
これらの定義は、節 2.1 と 2.3 の(対応する)クラス定義とは大変異なってい る。
extends
宣言は
reCellImp
のための donor オブジェクト
を表わす。
extends
に続くオブジェクトは静的に知っていると 仮定する。この宣言の結果として、
は
の attribute の 複製(状態は
とは異なる)を含むオブジェクトとなる。
override
の効果は、ホスト側で donor にあるメソッドを置換する。
embed
コンストラクタはまだ必要である。
オブジェクトは donor オブジェクトとは独立しているため
reCellImp
と
reCellExp
の定義は、スタンドアロン定義では 以下のように省略することができる。
object
reCell: ReCell
is
var
contents: Integer:=0;
var
backup: Integer:=0;
method
get(): Integer
is
return self
.contents
end;
method
set(n: Integer)
is
self
.backup:=
self
.contents;
self
.contents:=n;
end;
method
restore()
is
self
.contents:=
self
.backup
end;
end
また、以下のように定義することも可能である。
object
reCellImp1: ReCell
extends
cell
is
var
backup: Integer:=0;
method
restore()
is self
.contents:=
self
.backup
end;
end;
reCellImp1.set:=
method
(n: Integer)
is
self
.backup:=
self
.contents;
self
.contents:=n;
end;
:
Delegation
:
オブジェクトベース言語
:
Embedding による継承と Delegation による継承
平成12年8月22日