如何在静态类中使用多态或继承? [英] How to use polymorphism or inheritance in static classes?

查看:91
本文介绍了如何在静态类中使用多态或继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看,我知道静态类不能继承或实现.问题是到底什么是实现此功能的正确 C# + OOP 模式?".这"描述如下:

Look, I know static classes can't inherit or implement. The question is "what the heck is the right C# + OOP pattern to implement this?". "This" is described below:

我想为一组类定义一组通用的定义和实现,其中除了一个类型之外的所有类型都应该是静态的.也就是说,我想制作一些任意基本转换器,其中每个转换器都有完全相同的四个成员:

I want to define a common set of both definition and implementation for a group of classes where all but one type should be static. Namely, I want to make some arbitrary base converters where each have exactly the same four members:

// Theoritical; static classes can't actually implement
interface IBaseConverter { 
    int Base { get; }
    char[] Glyphs { get; }
    int ToInt(string value);
    string FromInt(int value);
}

// AND / OR (interface may be superfluous)
public class BaseConverter : IBaseConverter{ 
    public BaseConverter(int Base, char[] Glyphs) {
        this.Base = Base;
        this.Glyphs = Glyphs;
    }
    public int Base { get; private set; }
    public char[] Glyphs { get; private set;}
    public int ToInt(string value) { // shared logic...
    public string FromInt(int value) { // shared logic...
}

它们还可以基于 Base 的值和字形的有序集合共享完全相同的实现逻辑.例如,Base16Converter 将具有 Base = 16glyphs = { '0', '1', ... 'E', 'F' }.我相信 FromIntToInt 是不言自明的.显然我不需要为 base 16 实现转换器,但我确实需要为特定行业的 base 36(0 - Z Code 39 的字形).与诸如 [Convert]::ToInt32("123",16) 之类的内置转换和字符串格式化函数一样,这些都是静态方法——当基数和字形是 pre-确定.

They can also share the exact same implementation logic based on the value of Base and the ordered collection of glyphs. For example a Base16Converter would have Base = 16 and glyphs = { '0', '1', ... 'E', 'F' }. I trust the FromInt and ToInt are self-explanatory. Obviously I wouldn't need to implement a converter for base 16, but I do need to implement one for an industry-specific base 36 (the 0 - Z glyphs of Code 39). As with the built-in conversion and string formatting functions such as [Convert]::ToInt32("123",16) these are emphatically static methods -- when the base and glyphs are pre-determined.

我想保留一个可以用任意字形和基数初始化的实例版本,例如:

I want to keep an instance version that can be initialized with arbitrary glyphs and base, such as:

BaseConverter converter = new BaseConverter(7, new[]{ 'P', '!', 'U', '~', 'á', '9', ',' })
int anumber = converter.ToInt("~~!,U")  // Equals 8325

但我也想要 Base36Code39Converterstatic 类.另一种说法是,任何 static 实现者都只有硬编码的基础和字形:

But I also want a static class for the Base36Code39Converter. Another way of putting this is that any static implementers just have hard-coded base and glyphs:

// Theoritical; static classes can't inherit 
public static class Base36Code39Converter : BaseConverter {
    private static char[] _glyphs = { '0', '1', ... 'Z' }; 
    static Base36Code39Converter : base(36, _glyphs) { }
}

我明白为什么这对编译器不起作用——静态方法和所有这些都没有 vtable.我知道在 C# 中,静态类不能实现接口或从任何东西(对象除外)继承(见 为什么 C# 不允许静态方法实现接口?为什么我不能继承静态类?).

I can see why this wouldn't work for the compiler -- there is no vtable for static methods and all that. I understand that in C# a static class cannot implement interfaces or inherit from anything (other than object) (see Why Doesn't C# Allow Static Methods to Implement an Interface?, Why can't I inherit static classes?).

那么实现这一点的正确"C# + OOP 模式到底是什么?

So what the heck is the "right" C# + OOP pattern to implement this?

推荐答案

答案是单例模式.例如,请参见在 C# 中实现单例.

The answer was the Singleton pattern. See for example Implementing Singleton in C#.

Luiggi Mendoza 提供了这个答案,我将其标记为答案,但后来出于某种原因将其删除.为了完整起见,我重新发布它.

Luiggi Mendoza provided this answer, which I marked as an answer, but then he deleted it for some reason. I'm reposting it for completeness.

这篇关于如何在静态类中使用多态或继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆