派生クラスで基本クラスと同じシグネチャのメソッドを実装することが出来ますが、このときnewを使うかoverrideを使うかで動きが変わってきます。 結論を先に書いておくと・・・ newはフィールドの型に応じて呼び出しメソッドが決められる。 overrideはインスタンスの型に応じて呼び出しメソッドが決められる。 では、細かく見ていきましょう。 基本クラス(Base)とその派生クラス(Hoge)を作って、それぞれの動きを確認してみます。 まず、こんな感じで実装。 // 基本クラス class Base { public void CallNew() { Console.WriteLine("Base::CallNew"); } public virtual void CallOverride() { Console.WriteLine("Base::CallOverride"); } } /
C# 言語は、異なるライブラリ内の基底クラスと派生クラス間でのバージョン管理を進化させると同時に、下位互換性も維持されるよう設計されています。 そのため、たとえば、派生クラスのメンバーと同じ名前を使用して基底クラスの新規メンバーが導入されても、C# では完全にサポートされ、予期しない動作は発生しません。 ただしこのことは、メソッドが派生メソッドをオーバーライドするためのものなのか、それとも同じ名前の派生メソッドを非表示にする新規メソッドなのかを、クラスで明示的に記述しなければならないということでもあります。 C# では、派生クラスに基底クラスと同じ名前のメソッドを含めることができます。 派生クラスのメソッドの前に new または override キーワードがない場合、コンパイラは警告を発し、メソッドは new キーワードが存在する場合と同様に動作します。 派生クラスのメソッドの前に ne
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く