java - 将特定的 Java 枚举和类移植到 C#

标签 java c# enums super

我知道有一些关于此问题的帖子,但我找不到任何适合我的问题的帖子。

我开始用 Java 重新创建一个旧的 Flash 游戏,然后我切换到 Unity。

现在我一直致力于将包含 java 枚举的类移植到 C#

例如,我有以下类(class):

项目

武器(扩展元素)

Sword(扩展武器)Sword 包含枚举类型

    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 类中构建一个用于选择和初始化所述 Condition 的方法

    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 以便它适合我的项目

如果您的帖子可以为我的项目提供更多帮助,您可以在下面链接

注意:这是一个私有(private)项目,与任何作业或学习相关的内容无关

最佳答案

您可以使用以下代码在子级的构造函数中调用父级的构造函数。

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)

关于java - 将特定的 Java 枚举和类移植到 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60477335/

相关文章:

.net - 如何在 Entity Framework 中使用枚举?

java - 从 Ant 关闭 Java 程序?

java - 在 Android Studio 中合并 XML 和 Java 文件

java - 服务器生成的图像文件已损坏/不正确

java - 如果返回值取决于阻塞操作的结果,方法是否会阻塞?

c# - 多系统时钟

java - 在 Java 中通过 ref 传递枚举

c# - WebForm 的 NUnit 测试用例

c# - 在 select new 中使用 List<> 加入 LINQ

java - 当有很多子类时,代码是否优雅?