简体   繁体   English

我可以强制子类实现 static 解析方法吗?

[英]Can I force a subclass to implement a static parse-method?

I have a class hierarchy where all the implementing classes should have a range of static parse and parse-like methods.我有一个 class 层次结构,其中所有实现类都应该有一系列 static 解析和类似解析的方法。 Can I somehow "force" them to implement these?我可以以某种方式“强迫”他们实施这些吗? I know I can't use abstract static since that's not allowed.我知道我不能使用abstract static因为那是不允许的。 Is there any other way to make sure these methods are implemented?有没有其他方法可以确保实现这些方法?

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:(我之前曾建议static 接口纯粹作为泛型类型约束提供,这可能很有用 - 但它今天肯定不是 C# 的一部分,而且我没有任何信息表明有任何计划实施它:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM