我想知道,如果有人可以請給我講解一下:接口,繼承和「新」的關鍵字
class Program
{
static void Main()
{
AnotherDerivedClass d = new AnotherDerivedClass();
Console.WriteLine(d.PrintMessage());
IMsg m = d as IMsg;
//Why this prints BaseClass.
//How does it know that IMsg is implemented in the BaseClass.
Console.WriteLine(m.PrintMessage());
IMsg n = d as DerivedClass;
//Why this prints BaseClass and not DerivedClass
Console.WriteLine(n.PrintMessage());
Console.Read();
}
}
public interface IMsg
{
string PrintMessage();
}
public class BaseClass : IMsg
{
public string PrintMessage()
{
return "BaseClass";
}
}
public class DerivedClass : BaseClass
{
public new string PrintMessage()
{
return "DerivedClass";
}
}
public class AnotherDerivedClass : DerivedClass
{
public new string PrintMessage()
{
return "AnotherDerivedClass";
}
}
你有什麼問題? ...啊,我看到你的代碼中有評論:) – 2011-06-10 14:20:35
在問題主體中提問而不是在代碼評論中更好,因爲人們在代碼清單中尋找問題的方法...... – Oded 2011-06-10 14:21:47
I認爲如果我在代碼評論中提出問題,人們很容易理解我想要理解的內容。 – Asdfg 2011-06-10 14:24:59