Ben*_*nny 0 c# generics polymorphism
请考虑以下代码:
class Program
{
static void Main(string[] args)
{
var a = new A();
var b = new B();
Print(a);
Print(b);
Console.WriteLine(b.Hello);
Console.ReadLine();
}
static void Print<T>(T t) where T : A
{
Console.WriteLine(typeof(T));
Console.WriteLine(t.GetType());
Console.WriteLine(t.Hello);
}
}
public class A
{
public string Hello { get { return "HelloA"; } }
}
public class B : A
{
public new string Hello { get { return "HelloB"; } }
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出(.NET FW 4.5)
任何人都可以解释我是如何获得第二个HelloA的,因为我期待HelloB?
public new string Hello { get { return "HelloB"; } }
Run Code Online (Sandbox Code Playgroud)
该new关键字创建一个新函数,该函数恰好与旧函数同名.因此,B现在有两种方法:Hello(A),当通过编译时类型的变量调用时执行A,和Hello(B),当通过编译时类型的变量B(或其子类型)调用时执行.
由于您的泛型参数是T : A,编译器编译t.Hello为对Hello(A)的调用.
B 阴影(或隐藏)方法Hello而不是覆盖它.
你可能想写的是:
public class A
{
public virtual string Hello { get { return "HelloA"; } }
}
public class B : A
{
public override string Hello { get { return "HelloB"; } }
}
Run Code Online (Sandbox Code Playgroud)
请注意,base方法声明为virtual,而子类方法声明为override.