C#如何搜索最佳匹配重载方法

时间:2012-07-12 03:39:27

标签: c#

我有几个关于在C#中调用重载(或者我应该称之为隐藏)方法的问题。假设我有以下课程:

class ParaA {}
class ParaB : ParaA {}
class ParaC : ParaB {}

class TheBaseClass 
{
   public void DoJob (ParaA a){Console.WriteLine ("DoJob in TheBaseClass is being invoked");}

}

class TheDerivedClass : TheBaseClass 
{
  public void DoJob (ParaB b){Console.WriteLine ("DoJob in TheDerivedClass is being invoked");}
}

class Test
{
  //Case 1: which version of DoJob() is being called?
  TheDerivedClass aInstance= new TheDerivedClass ();
  aInstance.DoJob(new ParaA ());

  //Case 2: which version of DoJob() is being called?
  TheBaseClass aInstance= new TheDerivedClass ();
  aInstance.DoJob(new ParaA ());

  //Case 3: which version of DoJob() is being called?
  TheBaseClass aInstance= new TheDerivedClass ();
  aInstance.DoJob(new ParaB ());

  //Case 4: which version of DoJob() is being called?
  TheBaseClass aInstance= new TheDerivedClass ();
  aInstance.DoJob(new ParaC ());

}

我希望我已经清楚自己要做的事情。我想知道当调用者提供的参数与任何签名完全匹配但与某些签名兼容时,C#将如何搜索要调用的“匹配”版本的方法。当方法不仅仅在类中重载,而且隐藏,覆盖或由派生类重载时,它会让我更加困惑。上面的示例并未涵盖所有可能的情况。这有什么用语吗?

提前谢谢你们!

马修

2 个答案:

答案 0 :(得分:0)

Case2-4调用TheBaseClass只是因为DoJob不是虚方法且aInstance的类型是TheBaseClass

Case1调用TheBaseClass,因为它是直接匹配。

答案 1 :(得分:0)

我添加了一些代码来编译代码:

void Main()
{
    var t = new Test();
    t.Run();
}
class ParaA {}
class ParaB : ParaA {}
class ParaC : ParaB {}

class TheBaseClass 
{
   public void DoJob (ParaA a){Console.WriteLine ("DoJob in TheBaseClass is being invoked");}

}

class TheDerivedClass : TheBaseClass 
{
  public  void DoJob (ParaB b){Console.WriteLine ("DoJob in TheDerivedClass is being invoked");}
}

public class Test
{
    public void Run()
    {
        //Case 1: which version of DoJob() is being called?
        TheDerivedClass aInstance= new TheDerivedClass ();
        aInstance.DoJob(new ParaA ());

        //Case 2: which version of DoJob() is being called?
        TheBaseClass aInstance2= new TheDerivedClass ();
        aInstance2.DoJob(new ParaA ());

        //Case 3: which version of DoJob() is being called?
        TheBaseClass aInstance3= new TheDerivedClass ();
        aInstance3.DoJob(new ParaB ());

        //Case 4: which version of DoJob() is being called?
        TheBaseClass aInstance4= new TheDerivedClass ();
        aInstance4.DoJob(new ParaC ());
    }
}

产生输出:

DoJob in TheBaseClass is being invoked
DoJob in TheBaseClass is being invoked
DoJob in TheBaseClass is being invoked
DoJob in TheBaseClass is being invoked

即。每次调用基类方法。

在案例1中,调用它是因为参数是ParaA,而ParaA不是ParaB。 在其他情况下,调用它是因为对象实例的类型为'TheBaseClass'。

这是修改相同的代码以说明方法重载:

void Main()
{
    var t = new Test();
    t.Run();
}

class ParaA {}
class ParaB : ParaA {}
class ParaC : ParaB {}

class TheBaseClass 
{
   public virtual void DoJob (ParaA a){Console.WriteLine ("DoJob in TheBaseClass is being invoked");}

}

class TheDerivedClass : TheBaseClass 
{
  public override void DoJob (ParaA b){Console.WriteLine ("DoJob in TheDerivedClass is being invoked");}
}

public class Test
{
    public void Run()
    {
        //Case 1: which version of DoJob() is being called?
        TheDerivedClass aInstance= new TheDerivedClass ();
        aInstance.DoJob(new ParaA ());

        //Case 2: which version of DoJob() is being called?
        TheBaseClass aInstance2= new TheDerivedClass ();
        aInstance2.DoJob(new ParaA ());

        //Case 3: which version of DoJob() is being called?
        TheBaseClass aInstance3= new TheDerivedClass ();
        aInstance3.DoJob(new ParaB ());

        //Case 4: which version of DoJob() is being called?
        TheBaseClass aInstance4= new TheDerivedClass ();
        aInstance4.DoJob(new ParaC ());
    }
}

输出现在是:

DoJob in TheDerivedClass is being invoked
DoJob in TheDerivedClass is being invoked
DoJob in TheDerivedClass is being invoked
DoJob in TheDerivedClass is being invoked

每次调用TheDerivedClass方法,因为该对象的类型为“TheDerivedClass”。