如何在 C# 中强制继承类实现静态方法?
我想要做的就是确保类Item的子类实现静态方法,并且我希望在编译时检查它以避免运行时错误。
具有静态方法的抽象类似乎不起作用:
错误:静态成员 不能被标记为 覆盖、虚拟或抽象
public abstract class Item
{
public static abstract Item GetHistoricalItem(int id, DateTime pastDateTime);
}
public class Customer : Item
{
public static override Customer GetHistoricalItem(int id, DateTime pastDateTime)
{
return new Customer();
}
}
public class Address : Item
{
public static override Address GetHistoricalItem(int id, DateTime pastDateTime)
{
return new Address();
}
}
并且接口似乎也不起作用:
错误:客户未实现接口 成员GetHistoricalItem()
public class Customer : Item, HistoricalItem
{
public static Customer GetHistoricalItem(int id, DateTime pastDateTime)
{
return new Customer();
}
}
public class Address : Item, HistoricalItem
{
public static Address GetHistoricalItem(int id, DateTime pastDateTime)
{
return new Address();
}
}
interface HistoricalItem
{
Item GetHistoricalItem();
}
是否有一些解决方法可以让编译器检查继承类是否实现具有特定签名的静态方法?
All I want to do is make sure that child classes of the class Item implement a static method and I want this to be checked at compile time to avoid runtime errors.
abstract classes with static methods don't seem to work:
ERROR: A static member
cannot be marked as
override, virtual, or abstract
public abstract class Item
{
public static abstract Item GetHistoricalItem(int id, DateTime pastDateTime);
}
public class Customer : Item
{
public static override Customer GetHistoricalItem(int id, DateTime pastDateTime)
{
return new Customer();
}
}
public class Address : Item
{
public static override Address GetHistoricalItem(int id, DateTime pastDateTime)
{
return new Address();
}
}
and interfaces don't seem to work either:
ERROR: Customer does not implement interface
member GetHistoricalItem()
public class Customer : Item, HistoricalItem
{
public static Customer GetHistoricalItem(int id, DateTime pastDateTime)
{
return new Customer();
}
}
public class Address : Item, HistoricalItem
{
public static Address GetHistoricalItem(int id, DateTime pastDateTime)
{
return new Address();
}
}
interface HistoricalItem
{
Item GetHistoricalItem();
}
Is there some workaround for this to have the compiler check if inheriting classes implement a static method with a certain signature or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我为您的情况找到了一个解决方法:
希望这有帮助!
There is a workaround i figured out for your scenario:
Hope this helps!!
这是做不到的。
看看 为什么我不能在其中使用抽象静态方法c#?
This cannout be done.
Have a look at Why can’t I have abstract static methods in c#?
强制客户端实现静态方法是没有意义的——静态方法是“不可变的”。 (可能有更好的方法来描述它们,但这就是我现在能想到的全部!)
如果需要某种重写,我会考虑重新访问设计,可能使用某种形式的单例和注入组合。
It doesn't make sense to force clients to implement a static method - static methods are "immutable." (There's probably a better way to describe them but that's all my mind can come up with right now!)
If some sort of overriding is required, I would consider re-visiting the design, possibly using some form of a combination of singletons and injection.
似乎不可能,看看: 有没有办法强制 C# 类实现某些静态函数?
Seems it's not possible, take a look: Is there a way to force a C# class to implement certain static functions?
根据定义,静态方法不能在派生类中实现。
By definition a static method cannot be implemented in derived classes.