我可以强制子类实现静态解析方法吗?
我有一个类层次结构,其中所有实现类都应该具有一系列静态解析和类似解析的方法。我可以以某种方式“强迫”他们实施这些吗?我知道我不能使用abstract static
,因为这是不允许的。还有其他方法可以确保这些方法得到实施吗?
I have a class hierarchy where all the implementing classes should have a range of static parse and parse-like methods. Can I somehow "force" them to implement these? I know I can't use abstract static
since that's not allowed. Is there any other way to make sure these methods are implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,你不能。无论如何你都无法多态地调用它们。
您可以轻松做的最好的事情就是围绕此功能进行单元测试。
我之前建议过 静态接口 纯粹作为泛型类型约束使用,这可能很有用 - 但它肯定不是当今 C# 的一部分,而且我没有任何信息表明存在有任何实施计划吗:(
No, you can't. You wouldn't be able to call them polymorphically anyway.
The best you can easily do is to put unit tests around this functionality.
I've previously suggested static interfaces available purely as a generic type constraint, which could be useful - but it's certainly not part of C# today, and I have no information that there are any plans to implement it :(