我在以下实现中有错误.它说OnlineWebStore_Process无法实现IWebStore接口,因为它们没有匹配的返回类型.但该方法返回Item,它实现了在IWebStore接口中用作返回类型的IItem接口.什么是这个问题的好方法?
public interface IItem
{
string id { get; set; }
string name { get; set; }
}
public interface IWebStore
{
List<IItem> GetOnlineItems();
}
public class Item : IItem
{
public Item(string _id)
{
id = _id;
}
public string id { get; set; }
public string name { get; set; }
}
public class OnlineWebStore_Process : IWebStore
{
public List<Item> GetOnlineItems()
{
List<Item> items = new List<Item>();
return items
}
}
Run Code Online (Sandbox Code Playgroud)
public class OnlineWebStore_Process : IWebStore
{
public List<IItem> GetOnlineItems()
{
List<IItem> items = new List<IItem>();
return items;
}
}
Run Code Online (Sandbox Code Playgroud)
您的方法签名必须完全相同,您不能取代子类.如果你确实返回了一个子类,那么你将丢失一部分抽象,并且接口契约就会被破坏.