我已經添加了幾行,使代碼編譯:
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」。
您正在尋找「重載分辨率」,有關最佳細節,請參見[C#4.0語言規範](http://www.microsoft.com/en-us/download)中的第7.5.3節/details.aspx?id=7029)。至於你的哪些方法被調用,你可以通過你的程序來看看你自己。 [也見這些問題](http://stackoverflow.com/search?q=C%23+overload+resolution)。 – 2012-07-12 03:46:58
謝謝安東尼。這正是我正在尋找的 – Xinchao 2012-07-12 07:16:58