掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
親子孫継承したメソッド (ID:149714)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
回答がかぶったかも 親子孫の関係において孫が親や子の要素は継承したいけど そこで処理されていることは無視したいというのは無理です。 よく継承される代入メソッドAssignを操作すると意味がわかります。 下記に TOya TKo TMagoは正当後継者として書いたときのそれぞれのフィールド値の処理はこうなります。 TKoのAssignはTKoクラスが持つフィールドを代入しますが これを無視してTOyaの代入Assignメソッドだけ呼ぶのはおかしいことですね カプセル化されている意味が無くなるかと どうしてもそういう処理がしたいというのであれば 親クラスからの継承クラスとして定義して子クラスが持つ処理を全て実装する必要があります。 type TOya = class(TPersistent) private { Private 宣言 } FOyaData : Integer public { Public 宣言 } procedure Assign(Source : TPersistent);virtual; property OyaData : Integer read FOyaData write FOyaData end; type TKo = class(TOya) private { Private 宣言 } FKoData : Integer public { Public 宣言 } procedure Assign(Source : TPersistent);oveeride; property KoData : Integer read FKoData write FKoData end; type TMago = class(TKo) private { Private 宣言 } FMagoData : Integer public { Public 宣言 } procedure Assign(Source : TPersistent);oveeride; property MagoData : Integer read FMagoData write FMagoData end; type TMago2 = class(TOya) private { Private 宣言 } FKoData : Integer FMagoData : Integer public { Public 宣言 } procedure Assign(Source : TPersistent);oveeride; property KoData : Integer read FKoData write FKoData property MagoData : Integer read FMagoData write FMagoData end; procedure TOya.Assgin(Source: TPersistent); var a : TOya; begin if Source is TOya then begin a := TOya(Source); FOyaData := a.FOyaData; end else begin inherited; end; end; procedure TKo.Assgin(Source: TPersistent); var a : TKo; begin if Source is TKo then begin a := TKo(Source); FKoData := a.FKoData; inheried; end else begin inherited; end; end; procedure TMgo.Assgin(Source: TPersistent); var a : TMago; begin if Source is TMago then begin a := TMago(Source); FMagoData := a.FMagoData; inheried; end else begin inherited; end; end; procedure TMago2.Assgin(Source: TPersistent); var a : TMago2; begin if Source is TMago2 then begin a := TMago2(Source); FKoData := a.FKoData + 1; FMagoData := a.FMagoData; inheried; end else begin inherited; end; end;
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.