class Parent
{
public void foo()
{
Console.WriteLine("Hello from the foo inside Parent");
}
public virtual void bar()
{
Console.WriteLine("Hello from the bar inside Parent.");
}
public void foobar()
{
Console.WriteLine("Hello from the foobar inside Parent.");
}
}
class Child : Parent
{
public void foo()
{
base.foo();
Console.WriteLine("Hello from the foo inside Child");
}
public override void bar()
{
base.bar();
Console.WriteLine("Hello from the bar inside Child.");
}
public new void foobar()
{
base.foobar();
Console.WriteLine("Hello from the foobar inside Child.");
}
}
上面就是我使用來測試我在C#中繼承knowlege的代碼,但我這裏有一個困惑:孩子裏面C#繼承和壓倒一切的
- 功能
foo()
隱藏的foo()
家長。 - 功能
bar()
裏面的孩子隱藏父母的bar()
。 - 功能
foobar()
孩子隱藏在裏面的父母的bar()
()
所有三個功能使用不同的技術來隱藏父功能。任何人都可以請引導我,這三個之間有什麼不同,以及哪一個使用什麼時候?
請你解釋一下,在你的第二個代碼示例中,爲什麼/如何重寫被調用的方法,而我們甚至沒有實例化孩子?由於我對多態不熟悉,所以請不要介意我的問題對你來說是否愚蠢! –
@KamranAhmed我改變了樣品的順序,最後顯示最有趣的情況。所以,我想你是在問最後一個案子。實際上你在這兒實例化了一個小孩 - 'new Child()'是實例化的。但是您將對此實例的引用分配給基本類型的變量。 –