今日最近Stackoverflowで私はそれを学びました:
私はそれをすべて理解しようとしてきたので、コンストラクターを扱う私の主な質問をサポートする、別の非常に具体的な質問があります。
更新:質問全体を置き換えました:
TComputer = class(TObject)
public
constructor Create(Teapot: string='');
end;
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer); overload; virtual;
constructor Create(Cup: Integer; Teapot: string); overload; virtual;
end;
TCellPhoneを構築する場合、3つのコンストラクターを使用できます。
- カップ:整数
- カップ:整数; ティーポット:文字列
- [ティーポット:文字列='']
質問:なぜconstructor(Teapot: string='')
隠されていないのですか?
今私は3番目の子孫を追加しました:
TComputer = class(TObject)
public
constructor Create(Teapot: string='');
end;
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer); overload; virtual;
constructor Create(Cup: Integer; Teapot: string); overload; virtual;
end;
TiPhone = class(TCellPhone)
public
constructor Create(Cup: Integer); override;
end;
TiPhone
4つのコンストラクターを作成する場合は、次のようにします。
- カップ:整数
- カップ:整数
- カップ:整数; ティーポット:文字列
- [ティーポット:文字列='']
なぜコンストラクターが4つあるのですか?既存の3つのうちの1つを上書きしました。編集:これはコードインサイトのバグである可能性があり、4つ表示されますが、2つが同じである場合、どうすれば呼び出すことができますか?
元のコードを再度使用する:
TComputer = class(TObject)
public
constructor Create(Teapot: string='');
end;
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer); overload; virtual;
constructor Create(Cup: Integer; Teapot: string); overload; virtual;
end;
TCellPhone
3つのコンストラクターがあることはすでに知られています。
- カップ:整数
- カップ:整数; ティーポット:文字列
- [ティーポット:文字列='']
TCellPhone
の宣言を変更して祖先コンストラクターを非表示にするにはどうすればよいですか?たとえば、次のようになります。
TNokia = class(TCellPhone)
end;
コンストラクターは2つだけです。
- カップ:整数
- カップ:整数; ティーポット:文字列
ここで、がreintroduce
非仮想の祖先を非表示にするために使用される場合について説明します。前のケースでは、TiPhone
コンストラクターが4つあります(理想的には2つだけでTComputer
、祖先が何らかの形で隠されています)。しかし、私が修正できない場合でも、私は1つだけを持つようにTComputer
変更することができます:TiPhone
TComputer = class(TObject)
public
constructor Create(Teapot: string='');
end;
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer); overload; virtual;
constructor Create(Cup: Integer; Teapot: string); overload; virtual;
end;
TiPhone = class(TCellPhone)
public
constructor Create(Cup: Integer); reintroduce;
end;
現在TiPhone
、コンストラクターは1つだけです。
- カップ:整数
Reintroduceは通常、仮想祖先の非表示に関する警告を抑制するためにのみ使用されます。この場合:
Create(Teapot: string = '')
仮想ではありません-それでも、reintroduceを使用して非表示にすることができます。
しかし今、私が別のオーバーロードを追加した場合TiPhone
:
TiPhone = class(TCellPhone)
public
constructor Create(Cup: Integer); reintroduce; overload;
constructor Create(Handle: String); overload;
end;
その後、突然(以前は隠されていた)祖先が戻ってきます:
- TiPhone.Create(7);
- TiPhone.Create('ピンク');
- TiPhone.Create(7、'ピンク');
- TiPhone.Create();
ご覧のとおり、私はの論理を理解するのに苦労しています
- 何かが隠されているとき
- 何かを隠す方法
- 何かが表示されたとき
- 何かを示す方法