簡體   English   中英

將特定的 Java 枚舉和類移植到 C#

[英]porting specific Java Enums and Classes to C#

我知道有一些關於此的帖子,但我找不到任何適合我的問題的帖子。

我開始用 Java 重新創建一個舊的 Flash 游戲,然后我切換到了 unity。

現在我堅持將包含 Java 枚舉的類移植到 C#

例如,我有以下課程:

物品

武器(擴展項目)

劍(擴展武器)劍包含枚舉類型

    enum Type{
        //Name           min max price 2hand   baselvl  ranged
        Falchion        ( 1,  4,  324, false, (byte)  1, false),
        Shortsword      ( 1,  5,  346, false, (byte)  1, false),
        Cutlass         ( 2,  6,  411, false, (byte)  3, false),
        Throwingknife   ( 2,  9,  478, false, (byte)  3,  true), //RANGED
        Longsword       ( 2,  8,  567,  true, (byte)  5, false), //TWOHANDED
        Broadsword      ( 4,  8,  690, false, (byte)  7, false),
        Katana          ( 4, 11,  807,  true, (byte)  9, false), //TWOHANDED
        Saber           ( 7, 12, 1210, false, (byte) 10, false),
        Flamberge       ( 8, 17, 1623,  true, (byte) 13, false), //TWOHANDED
        Lightsaber      (12, 20, 2732, false, (byte) 15, false);

        final int minDMG;   
        final int maxDMG;
        final int price;
        final byte baseLVL;
        final boolean twoHanded;
        final boolean ranged;

        private Type(int minD, int maxD, int p, boolean twoHa, byte bLVL, boolean r) {
            this.minDMG = minD;
            this.maxDMG = maxD;
            this.price = p;
            this.twoHanded = twoHa;
            this.baseLVL = bLVL;
            this.ranged = r;
        }

        public static final Type[] types = Type.values();
        public static Type getType(int i) {
            return Type.types[i];   
        }
     }

對於我使用的劍的構造函數:

    public Sword(int Index) {

        super("Sword",2,""+types[Index], types[Index].minDMG,types[Index].maxDMG,types[Index].price,types[Index].twoHanded,types[Index].baseLVL,types[Index].ranged);   
    }

武器:

    public Weapon(String wCl, int wCID, String n,int minD, int maxD, int p, boolean twoHa, byte bLVL, boolean r) {
        super("Weapon",n,minD,maxD,p,twoHa, bLVL,r);
        this.weaponClass        = wCl;
        this.weaponClassID  = wCID;     
    }

最后項目:

public Item(String iC, String n,int minD, int maxD, int p, boolean twoHa, byte bLVL,boolean r) {
        //Weapon
        this.condition = initCondition();
        this.name           = n;
        this.minDMG         = minD  * this.condition.multiplier + ((this.condition.multiplier - 1) * 1);
        this.maxDMG         = maxD  * this.condition.multiplier + ((this.condition.multiplier - 1) * 1);
        this.price          = p     * this.condition.multiplier*13;
        this.twohanded      = twoHa;
        this.bodypart       = "Hands";
        this.ranged = r;
}

初始化它我可以寫

Item i = new Sword(0);

我在 C# 中找不到好的解決方案

我用於 Item 類中的 Condition 枚舉的第一個解決方案是創建一個新的 c# 類“Condition”

public class Condition
{
    readonly string name;
    readonly int multiplier;
    readonly string color = "GREY";

    public Condition(string n, int m, string c) {
        this.name = n;
        this.multiplier = m;
        this.color = c;
    }

然后在 Item 類中構建一個用於選擇和初始化所述條件的方法

    public Condition condition;

    public Item(int _luck)
    {
        this.condition = generateCondition(conditionSelector(_luck));
    }
private Condition generateCondition(int selector)
    {
        if      (selector ==  0)                   return new Condition("Godlike"   , 15, "YELLOW"   ); //  0        -->  1%    MAX LVL LUCK Percentage:  0      -->  2%
        else if (selector >=  1 && selector <=  2) return new Condition("Legendary" , 10, "WHITE"    ); //  1 -   2  -->  2%    MAX LVL LUCK Percentage:  1 -  2 -->  4%
        else if (selector >=  3 && selector <=  5) return new Condition("Mythical"  ,  8, "PURPLE"   ); //  3 -   5  -->  3%    MAX LVL LUCK Percentage:  3 -  5 -->  6%
        else if (selector >=  6 && selector <=  9) return new Condition("Ultra Rare",  5, "DARKBLUE" ); //  6 -    9 -->  4%    MAX LVL LUCK Percentage:  6 -  9 -->  8%
        else if (selector >= 10 && selector <= 14) return new Condition("Rare"      ,  3, "LIGHTBLUE"); //  10 -  14 -->  5%    MAX LVL LUCK Percentage: 10 - 14 --> 10%
        else if (selector >= 15 && selector <= 20) return new Condition("Uncommon"  ,  2, "GREEN"    ); //  15 -  20 -->  6%    MAX LVL LUCK Percentage: 15 - 20 --> 12%
        else if (selector >= 15 && selector <= 20) return new Condition("Common"    ,  1, "GREY"     ); //  21 -  99 --> 79%    MAX LVL LUCK Percentage: 21 - 49 --> 58%

        return new Condition("Common", 1, "GREY");
    }

這適用於項目的條件部分。

但我沒有找到將其他類移植到 C# 的好方法

而且我不知道如何使用super等效base使其適合我的項目

如果你有帖子可以幫助我更多地完成我的項目,你可以在下面鏈接它

注意:這是一個私人項目,與任何家庭作業或學習相關的東西無關

您可以使用以下代碼在 Child 的構造函數中調用 Parent 的構造函數。

public Sword(int index):base(index)

如果你想修改索引並發送其他東西,那么你可以調用一個在構造函數中完成工作的函數。

public Sword(int index):base(somefunction(index))

在您的情況下,您需要使用“index”變量將所有新參數集一起發送。

public Sword(int Index):base("Sword",2,""+types[Index], types[Index].minDMG,types[Index].maxDMG,types[Index].price,types[Index].twoHanded,types[Index].baseLVL,types[Index].ranged)

我知道這有點舊,但是,我想盡可能匹配 C# 中的 Java 枚舉。 如果您查看這篇文章,我相信這很接近Java Like enum ,我認為您可以使用它將代碼從 Java 移植到 C#

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM