我有一个类别Item
,它是由其他类继承... Attraction
,Business
,CommunityEvent
。我不能以我的方式设置对象items
,这是一个语法错误。列表不能像这样分配。做这件事的最好方法是什么?基本上,吸引力是一个项目,但它有更多的属性。商业是一个项目,但它有更多的属性。 ...等等。C#继承/接口
public List<Item> GetItems(PageType type)
{
List<Item> items;
switch (type)
{
case PageType.Attraction:
items = new List<Attraction>();
break;
case PageType.Business:
case PageType.ShopAndEat:
items = new List<Business>();
break;
case PageType.Event:
items = new List<CommunityEvent>();
break;
default:
items = new List<Item>();
break;
}
// TODO: Call Webservice method to get Item
return items;
}
您可以使项目成为IEnumerable- 并返回IEnumerable
- 。 –
FreddyFlares