考虑下面的类和接口:泛型和接口问题
public class Test<T>
{
}
public interface ITesting<T>
{
Test<T> LoadTest();
}
我可以添加一个类有以下:
public class TestManager : ITesting<object>
{
#region ITesting
public Test<object> LoadTest()
{
return new Test<object>();
}
#endregion
}
的正常工作和不引发错误。如果我尝试用下面的来替代类:
public class TestDerived : Test<object>
{
}
public class TestDerivedManager : ITesting<object>
{
#region ITesting
public TestDerived LoadTest()
{
return new TestDerived();
}
#endregion
}
我现在得到以下错误:
Error 'TestDerivedManager' does not implement interface member 'ITesting.LoadTest()'. 'TestDerivedManager.LoadTest()' cannot implement 'ITesting.LoadTest()' because it does not have the matching return type of 'Test'.
但对我来说,好像这应该工作作为TestDerived
是Test<object>
。我显然在这里没有正确理解某些东西。有人可能指出我为什么这是不正确的细节?可能我会怎样做才能纠正它?
我注意到,这种行为与泛型没有任何关系。如果你有'interface I {Animal M(); } class C:I {public Giraffe M(){return new Giraffe(); }}'你会得到同样的错误。 – 2013-05-13 15:00:59