如何在 C# 中强制继承类实现静态方法?

发布于 2024-08-14 17:38:20 字数 1265 浏览 5 评论 0原文

我想要做的就是确保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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

找回味觉 2024-08-21 17:38:20

我为您的情况找到了一个解决方法:

public class Customer : Reference<Customer>, IHistoricalItem
{
}

public class Address : Reference<Address>, IHistoricalItem
{
}

public interface IHistoricalItem
{
}

public class Reference<T> where T : IHistoricalItem, new()
{
    public static T GetHistoricItem(int id, DateTime pastDateTime)
    {
        return new T();
    }
}

希望这有帮助!

There is a workaround i figured out for your scenario:

public class Customer : Reference<Customer>, IHistoricalItem
{
}

public class Address : Reference<Address>, IHistoricalItem
{
}

public interface IHistoricalItem
{
}

public class Reference<T> where T : IHistoricalItem, new()
{
    public static T GetHistoricItem(int id, DateTime pastDateTime)
    {
        return new T();
    }
}

Hope this helps!!

梦行七里 2024-08-21 17:38:20

强制客户端实现静态方法是没有意义的——静态方法是“不可变的”。 (可能有更好的方法来描述它们,但这就是我现在能想到的全部!)

如果需要某种重写,我会考虑重新访问设计,可能使用某种形式的单例和注入组合。

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.

桃扇骨 2024-08-21 17:38:20

根据定义,静态方法不能在派生类中实现。

By definition a static method cannot be implemented in derived classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文