看起来在以下情况下,多态性无法正常工作我有以下定义:
interface BaseInterface{}
interface NewInterface:BaseInterface{}
class NewClass:NewInterface{}
class GenericClass<T> where T:BaseInterface
{
public string WhoIAm(T anObject)
{
return TestPolymorphism.CheckInterface(anObject);
}
}
class ImplementedClass:GenericClass<NewInterface>{}
class TestPolymorphism
{
public static string CheckInterface(BaseInterface anInterface)
{
return "BaseInterface";
}
public static string CheckInterface(NewInterface anInterface)
{
return "NewInterface";
}
}
然后当我打电话时:
NewClass nc = new NewClass();
ImplementedClass impClass = new ImplementedClass();
Console.WriteLine("The result is " + impClass.WhoIAm(nc));
我有“结果是 BaseInterface”
我期待“结果是 NewInterface”作为 nc 实现 BaseClass 和NewClass
什么是获得“NewClass”结果的最佳方法?
谢谢