-2

我有一个来自 BioJava 库的 Java 枚举器缩短为以下内容:

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#,其中有一个元素的枚举,并附有 Element 的基本信息。我该怎么做?

优选地,该解决方案将是内存有效的。

4

1 回答 1

2

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;


    }
}
于 2013-09-17T14:31:15.720 回答