[英]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.