1
例如,在下面的類中,假設Say()
是一個相對較長的方法。其他一切都很好,但我想爲GetWords()
做點別的事情。我創建了一個繼承類,並使GetWords()
做別的。但其Say()
方法仍將使用父類的GetWords()
。方法B用於方法A;在繼承類中,使A被覆蓋B
有沒有辦法在繼承類中複製並粘貼方法體而不覆蓋Say()
? Dog
類已經實現了,但我可以根據需要隨時更改它。
Doge d = new Doge();
d.Say(); //says `Rrrrrrrr`.
public class Dog
{
public void Say()
{
// Do a lot of stuff
var words = GetWords();
Debug.WriteLine(words);
// Do a lot of other stuff
}
protected string GetWords()
{
return "Rrrrrrrr";
}
}
public class Doge:Dog
{
protected new string GetWords()
{
return "Such inheritance";
}
}
所以,你希望它打印出來'這樣的inheritance'? –
是的,對於這個例子。 –