我的问题很简单,但我找不到按照我希望的方式实现代码的方法.所以我开始想知道我想要实现的代码是不是很好.如果是,那么最好的方法是什么.
在这里:
class InputManager { SortedDictionary<ushort,Keys> inputList = new SortedDictionary<ushort,Keys>(); public void Add(ushort id,Keys key) {...} public bool Ispressed(ushort id) {...} } class Main { private enum RegisteredInput : ushort { Up,Down,Confirm } public Main() { InputManager manager = new InputManager(); manager.Add(RegisteredInput.Up,Keys.Q); manager.Add(RegisteredInput.Down,Keys.A); manager.Add(RegisteredInput.Confirm,Keys.Enter); } void update() { if(manager.Ispressed(RegisteredInput.Up)) action(); } }
The best overloaded method match for ‘InputManager.Add(ushort,Keys)’ has some invalid arguments
Argument ‘1’: cannot convert from ‘RegisteredInput’ to ‘ushort’
如果我使用像manager.Add((ushort)RegisteredInput.Up,Keys.Q);它会工作.但是因为演员必须是明确的,我想知道它是不是在C#中推荐的代码,就像它在C中并且是否有更好的方法(比如对每个值使用const ushort,我有点不喜欢)许多).
到目前为止我得到的最佳答案是this thread,但听起来很像黑客,我很担心.
谢谢!
解决方法
使InputManager成为通用类型. IE:
class InputManager<T> { SortedDictionary<T,Keys> inputList = new SortedDictionary<T,Keys>(); public void add(T id,Keys key) {...} public bool ispressed(T id) {...} }
原文地址:https://www.jb51.cc/csharp/98647.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。