Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将特定Java枚举和类移植到C#_Java_C#_Enums_Super - Fatal编程技术网

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

将特定Java枚举和类移植到C#,java,c#,enums,super,Java,C#,Enums,Super,我知道有很多关于这方面的帖子,但我找不到任何适合我问题的帖子 我开始用Java重新创建一个旧的flash游戏,然后我转向unity 现在我不得不将包含java枚举的类移植到C# 例如,我有以下课程: 项目 武器(扩展项) 剑(扩展武器)剑包含枚举类型 enum Type{ //Name min max price 2hand baselvl ranged Falchion ( 1, 4, 324, false,

我知道有很多关于这方面的帖子,但我找不到任何适合我问题的帖子

我开始用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类中用于条件枚举的第一个解决方案是创建一个新的c#class“Condition”

然后在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.condition=generateCondition(条件选择器(_-luck));
}
专用条件generateCondition(整数选择器)
{
如果(选择器==0)返回新条件(“类神”,15,“黄色”);//0-->1%最大LVL运气百分比:0-->2%
否则,如果(选择器>=1&&选择器2%最大LVL运气百分比:1-2-->4%
否则如果(选择器>=3&&选择器3%最大LVL运气百分比:3-5-->6%
否则(选择器>=6&&选择器4%最大LVL运气百分比:6-9-->8%
否则,如果(选择器>=10&&选择器5%最大LVL运气百分比:10-14-->10%
否则如果(选择器>=15&&选择器6%最大LVL运气百分比:15-20-->12%
否则(选择器>=15&&选择器79%最大LVL运气百分比:21-49-->58%
返回新条件(“普通”,1,“灰色”);
}
这适用于项目的条件部分

但是我没有找到一个好方法将其他类移植到C#

我不知道如何使用
super
等效的
base
,使其适合我的项目

如果你有帖子,可以帮助我更多的我的项目,你可以链接到下面


注意:这是一个私人项目,与任何家庭作业或学习相关的内容无关

您可以使用下面的代码在孩子的构造函数中调用家长的构造函数

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移植到C#

但是我在哪里创建C#中的类型数组?我需要创建类SwordManager然后创建一个SwordManager吗在Java中,你在哪里创建了类型数组?`public static final type[]types=type.Values();public static type getType(int i){return type.types[i];}'第一个代码段我假设类型是一个全局变量。在c#中也可以这样做。
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)