限制通用子类方法可以在C#中接受的类型

编程入门 行业动态 更新时间:2024-10-19 12:39:30
本文介绍了限制通用子类方法可以在C#中接受的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果标题不明确或重复,我深表歉意.今天,我花了大量的时间在泛型上阅读并寻找类似的情况,但无济于事.

I apologise if the title is ambiguous or if this is a duplicate. I've spent today reading a ton on generics and looking for a similar situation to no avail.

我正在编写一个小游戏引擎.这方面的一个方面是单位"携带资源的能力.有些单位可以携带任何资源,有些单位只能携带某些专门资源.为此,我具有以下结构:

I'm writing a little game engine. One aspect of this is the ability for a 'unit' to carry resources. Some units can carry any resource, some are restricted to carrying certain specialised resources. To this end I have the following structure:

Resource基类:

public abstract class Resource { private int _count; public int Count { get { return _count; } set { _count = value; } } public Resource(int resourceCount) { _count = resourceCount; } }

然后是Resource专业化Wood:

public class Wood : Resource { public Wood(int resourceCount) : base(resourceCount) { } }

然后我有了通用的ResourceStore:

public class ResourceStore<T> : IResourceStore where T : Resource { private List<Resource> _store; public IEnumerable<Resource> Store { get { return _store; } } public void AddResource(Resource resource) { _store = new List<Resource>(); _store.Add(resource); } }

,最后是专门的商店WoodStore,其中AddResource方法应只接受Wood:

and finally a specialised store WoodStore where the AddResource method should only accept Wood:

public class WoodStore : ResourceStore<Wood> { }

为了完整起见,接口IResourceStore由可以充当资源存储库的任何东西实现:

For completeness the interface IResourceStore which is implemented by anything that can act as a Resource Store:

public interface IResourceStore { void AddResource(Resource resource); }

如果我运行一个小的控制台应用程序并执行以下操作,则在尝试将Wheat添加到WoodStore时会出现错误.但事实并非如此,实际上输出显示WoodStore现在包含木材和小麦.这是控制台应用程序:

If I run a little console application and do the following I would have expected an error when trying to add Wheat to a WoodStore. But it doesn't and in fact the output shows that the WoodStore now contains Wood and Wheat. This is the console app:

class Program { static void Main(string[] args) { WoodStore store = new WoodStore(); store.AddResource(new Wood(10)); store.AddResource(new Wheat(5)); foreach (Resource resource in store.Store) { Console.WriteLine("{0} {1}", resource.GetType(), resource.Count); } Console.ReadLine(); } }

最后,这里是Wheat,只是为了完整性,尽管没有什么特别之处:

Finally here is Wheat just for completeness although there is nothing special about it:

public class Wheat : Resource { public Wheat(int resourceCount) : base(resourceCount) { } }

我显然在这棵树上叫错了树,希望对我如何限制WoodStore只接受Wood提供任何帮助.最终,会有很多不同的商店受到一定的限制,我正在寻找一种通用的处理方法.

I'm obviously barking up the wrong tree on this one, and would appreciate any help as to how I would go about restricting WoodStore to only accept Wood. Ultimately there will be a lot of different stores that would have certain restrictions and I'm looking for a way to handle this generically.

非常感谢.

推荐答案

您的接口需要通用,您需要在后备存储中使用T,而且AddResource中的new List也不是您想要的我敢打赌:

Your interface needs to be generic and you need to use T in the backing store, also the new List in AddResource is not what you want I bet:

public interface IResourceStore<T> where T : Resource { void AddResource(T resource); } public class ResourceStore<T> : IResourceStore<T> where T : Resource { private List<T> _store = new List<T>(); public IEnumerable<T> Store { get { return _store; } } public void AddResource(T resource) { //_store = new List<T>(); //Do you really want to create a new list every time you call AddResource? _store.Add(resource); } }

更多推荐

限制通用子类方法可以在C#中接受的类型

本文发布于:2023-11-11 23:15:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子类   类型   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!