C#使用分层参数重载分辨率

Bol*_*lha 4 c# methods overloading resolution

在示例中,我期望获得第三个输出,因为第三类与Print调用具有精确的签名匹配,但输出为Second.为什么会这样?

class First
{
    public virtual void Print(string x)
    {
        Console.WriteLine("First");
    }
}

class Second : First
{
    public void Print(object x)
    {
        Console.WriteLine("Second");
    }
}

class Third : Second
{
    public override void Print(string x)
    {
        Console.WriteLine("Third");
    }
}

class Program
{
    static void Main(string[] args)
    {
        string sss = "lalala";
        Third t = new Third();    
        t.Print(sss);

        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

Iva*_*lov 5

看到这里:

...方法调用的候选集不包括标记为override的方法(第7.3节),如果派生类中的任何方法适用,则基类中的方法不是候选方法(第7.5.5.1节).

因此,覆盖方法Third不适用并Second.Print(object)隐藏基类实现并成为单个候选者.