简体   繁体   English

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

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

I know there are som posts about this, but i couldnt find any post that fits specificly to my problem.我知道有一些关于此的帖子,但我找不到任何适合我的问题的帖子。

I started to recreate an old flash game in Java, then i switched to unity.我开始用 Java 重新创建一个旧的 Flash 游戏,然后我切换到了 unity。

Now i am stuck with porting my Classes, that contain java enums to C#现在我坚持将包含 Java 枚举的类移植到 C#

For example i have the following classes:例如,我有以下课程:

Item物品

Weapon (extends Item)武器(扩展项目)

Sword (extends Weapon) Sword contains enum Type剑(扩展武器)剑包含枚举类型

    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];   
        }
     }

for the Constructor of Sword I use:对于我使用的剑的构造函数:

    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);   
    }

Weapon:武器:

    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;     
    }

And finally Item:最后项目:

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;
}

to initialize it i can just write初始化它我可以写

Item i = new Sword(0);

I couldnt find a good solution in C#我在 C# 中找不到好的解决方案

my first solution that i used for the Condition enum in the Item class was creating a new c# class "Condition"我用于 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;
    }

and then building a method for selecting and initializing said Condition inside the Item class然后在 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");
    }

This works fine for the condition part of the item.这适用于项目的条件部分。

but I did not find a good way to port the other classes to C#但我没有找到将其他类移植到 C# 的好方法

and I don't know how I can use the super equivalent base so that it fits my project而且我不知道如何使用super等效base使其适合我的项目

If you have posts that can help me more with my project you can link it below如果你有帖子可以帮助我更多地完成我的项目,你可以在下面链接它

Note: this is a private project and not related to any homework or studyrelated stuff注意:这是一个私人项目,与任何家庭作业或学习相关的东西无关

You can call Parent's constructor in Child's constructor using below code.您可以使用以下代码在 Child 的构造函数中调用 Parent 的构造函数。

public Sword(int index):base(index)

If you want modify the index and send something else then you can call a function which does the job in the constructor.如果你想修改索引并发送其他东西,那么你可以调用一个在构造函数中完成工作的函数。

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

In your case, you need to send all together new set of parameters using "index" variable.在您的情况下,您需要使用“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)

I know this is a bit old but, I wanted to match the Java enum in C# as much as possible.我知道这有点旧,但是,我想尽可能匹配 C# 中的 Java 枚举。 If you check out this post, I believe that this comes close Java Like enum and I think you can use this to port your code from Java to C#如果您查看这篇文章,我相信这很接近Java Like enum ,我认为您可以使用它将代码从 Java 移植到 C#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM