将 Java 枚举器移植到 C#

本文关键字:Java 枚举 | 更新日期: 2023-09-27 18:31:49

我有一个Java枚举器,从BioJava库中缩短为以下内容:

public enum Element implements Serializable {
// most frequently used elements first
H(1, 1, 39, 1.10f, 0.32f, 1, 1, 1, 1, 1, 1.008f, 0, 1, 2.20f, ElementType.OTHER_NONMETAL),
C(6, 2, 0, 1.55f, 0.77f, 4, 4, 4, 4, 4, 12.011f, 2, -4, 2.55f, ElementType.OTHER_NONMETAL),
N(7, 2, 57, 1.40f, 0.75f, 5, 2, 5, 3, 4, 14.007f, 2, -3, 3.04f, ElementType.OTHER_NONMETAL),
O(8, 2, 65, 1.35f, 0.73f, 6, 1, 2, 2, 2, 16.000f, 2, -2, 3.44f, ElementType.OTHER_NONMETAL),
.....

private int atomicNumber;
private int period;
private float VDWRadius; // in Angstroms
private float covalentRadius; // in Angstroms
private int valenceElectronCount;
private int minimumValence;
private int maximumValence;
private int commonValence;
private int maximumCovalentValence;
private float atomicMass;
private int coreElectronCount;
private int oxidationState;
private float paulingElectronegativity;
private ElementType elementType;

private static final Map<String,Element> allElements ;
static {
    allElements = new HashMap<String,Element>();
    for (Element e : Element.values()){
        allElements.put(e.toString().toLowerCase(), e);
    }
}
private Element(int atomicNumber,
        int period,
        int hillOrder,
        float VDWRadius,
        float covalentRadius,
        int valenceElectronCount,
        int minimumValence,
        int maximumValence,
        int commonValence,
        int maximumCovalentValence,
        float atomicMass,
        int coreElectronCount,
        int oxidationState,
        float paulingElectronegativity,
        ElementType elementType) {
    this.atomicNumber = atomicNumber;
    this.period = period;
    //this.hillOrder = hillOrder;
    this.VDWRadius = VDWRadius;
    this.covalentRadius = covalentRadius;
    this.valenceElectronCount = valenceElectronCount;
    this.minimumValence = minimumValence;
    this.maximumValence = maximumValence;
    this.commonValence = commonValence;
    this.maximumCovalentValence = maximumCovalentValence;
    this.atomicMass = atomicMass;
    this.coreElectronCount = coreElectronCount;
    this.oxidationState = oxidationState;
    this.paulingElectronegativity = paulingElectronegativity;
    this.elementType = elementType;

}

我想简单地将此结构移植到 C#,其中枚举了元素,并附加了元素的基本信息。 我该怎么做?

优选地,解决方案将是内存效率高的。

将 Java 枚举器移植到 C#

CLR 中的枚举只是命名为常量。底层 类型必须是整数。在Java中,枚举更像是一个命名的 类型的实例。这种类型可能非常复杂,并且 - 作为您的 示例显示 - 包含各种类型的多个字段。

要将示例移植到 C#,我只需将枚举更改为不可变 类并公开该类的静态只读实例:

public class Element {
    // most frequently used elements first
    public static readonly Element H = new Element(1, 1, 39, 1.10f, 0.32f, 1, 1, 1, 1, 1, 1.008f, 0, 1, 2.20f, ElementType.OTHER_NONMETAL);
    public static readonly Element C = new Element(6, 2, 0, 1.55f, 0.77f, 4, 4, 4, 4, 4, 12.011f, 2, -4, 2.55f, ElementType.OTHER_NONMETAL);
    public static readonly Element N = new Element(7, 2, 57, 1.40f, 0.75f, 5, 2, 5, 3, 4, 14.007f, 2, -3, 3.04f, ElementType.OTHER_NONMETAL);
    public static readonly Element O = new Element(8, 2, 65, 1.35f, 0.73f, 6, 1, 2, 2, 2, 16.000f, 2, -2, 3.44f, ElementType.OTHER_NONMETAL);

    private int atomicNumber;
    private int period;
    private float VDWRadius; // in Angstroms
    private float covalentRadius; // in Angstroms
    private int valenceElectronCount;
    private int minimumValence;
    private int maximumValence;
    private int commonValence;
    private int maximumCovalentValence;
    private float atomicMass;
    private int coreElectronCount;
    private int oxidationState;
    private float paulingElectronegativity;
    private ElementType elementType;

    private static readonly IDictionary<String,Element> allElements ;
    static Element(){
        allElements = new Dictionary<String,Element>();
        allElements.Add("h", H);
        allElements.Add("c", C);
        allElements.Add("n", N);
        allElements.Add("o", O);
    }
    private Element(int atomicNumber,
            int period,
            int hillOrder,
            float VDWRadius,
            float covalentRadius,
            int valenceElectronCount,
            int minimumValence,
            int maximumValence,
            int commonValence,
            int maximumCovalentValence,
            float atomicMass,
            int coreElectronCount,
            int oxidationState,
            float paulingElectronegativity,
            ElementType elementType) {
        this.atomicNumber = atomicNumber;
        this.period = period;
        //this.hillOrder = hillOrder;
        this.VDWRadius = VDWRadius;
        this.covalentRadius = covalentRadius;
        this.valenceElectronCount = valenceElectronCount;
        this.minimumValence = minimumValence;
        this.maximumValence = maximumValence;
        this.commonValence = commonValence;
        this.maximumCovalentValence = maximumCovalentValence;
        this.atomicMass = atomicMass;
        this.coreElectronCount = coreElectronCount;
        this.oxidationState = oxidationState;
        this.paulingElectronegativity = paulingElectronegativity;
        this.elementType = elementType;

    }
}