1

「オーバーライド」+「仮想」がC#で行うように、完全にオーバーライドせずにサブクラスのメソッドにコードを追加する方法はありますか?オーバーライド メソッドで重複したコードを書いていることに気づきました。そして、それについて何をすべきかわからない

4

1 に答える 1

3

override と virtual を別のメカニズムで使用できます。例えば、

class MyBase
    {
        private int MyVar;
        public virtual void DoStuff(int i , int j)
        {
            MyVar = i + j; //This is your common code which is added in base class

        }
    }


    class OverridClass : MyBase
    {
        private int MyNewCount;
        public override void DoStuff(int i, int j)
        {
            MyNewCount = i + j;
            base.DoStuff(i, j); //This is how you reuse your common code and write the code which is more specific to this method

        }
    }`
于 2013-09-28T14:32:29.003 に答える