我想知道,如果有人可以请给我讲解一下:接口,继承和“新”的关键字
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