Confused about “override” vs. “new” in C#

前端 未结 4 1055
粉色の甜心
粉色の甜心 2020-12-09 16:03

I\'m having the following classes:

class Base
{
    public virtual void Print()
    {
        Console.WriteLine(\"Base\");
    }
}

class Der1 : Base
{
    p         


        
4条回答
  •  温柔的废话
    2020-12-09 16:21

    As everyone has said the class Der1 is replacing Print() instead of overriding it. To see this in action you could base d1 and d2 to Base and then call the print method. It will then return Base.

    ((Base)d2).Print(); //Base
    

提交回复
热议问题