C#重写方法和隐藏方法

Man man = new Man();
Person person = man as Person;
Console.WriteLine(person.GetNum());
Console.WriteLine(man.GetNum());
Woman woman = new Woman();
Person person1 = woman as Person;

Console.WriteLine(woman.GetNum());
Console.WriteLine(person1.GetNum());


class Person { 

    public virtual int GetNum()
    {
        return 1;
    }
}
class Man : Person
{

    public override int GetNum() { return 2; }
}
class Woman : Person
{

    public new int GetNum() { return 3; }
}

你可能感兴趣的:(c#,开发语言)