2015-03-02 179 views
0

我正在尝试使用Java代码为我的世界做一个mod,并且看到我并没有询问与minecraft直接相关的东西,我想我会在这里问它。Java:我如何将变量的名称放入字符串中?

里面我的代码,我有 renderItem.getItemModelMesher().register(copperIngot, 0, new ModelResourceLocation(RefStrings.MODID + ":" + ((CopperIngot) copperIngot).getName(), "inventory"));

在该行调用该函数的getName(),它位于该文件CopperIngot的结尾,并返回字符串“copperingot”。我想要做的是获取变量“copperIngot”的名称,并将其设置为名为字符串内的小写字母。

我这样做的原因是,这样我可以具有1个文件锭,1个文件块等(块和锭作为变量类型的块和Item它们在底座的Minecraft代码创建)

现在,我使用下面的代码张贴tinker_fairy

`Field[] fields = MainRegistry.class.getDeclaredFields(); 
//gives no of fields 
System.out.println(fields.length);   
for (Field field : fields) { 
    //gives the names of the fields 
    System.out.println(field.getName()); 
}` 

,给了我一个控制台读出这样的:

[com.hihellobyeoh.derptech.main.MainRegistry:INIT:6 3]:instance [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:proxy [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:tabDerptech [com.hihellobyeoh.derptech.main。 MainRegistry:init:63]:scandiumIndot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:scandiumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:scandiumBlock [com.hihellobyeoh。 depttech.main.MainRegistry:init:63]:titaniumIngot [titanium.touch [ com.hihellobyeoh.derptech.main.MainRegistry:初始化:63]:vanadiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:初始化:63]:范adiumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:vanadiumBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:chromiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init :63]:chromiumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:chromiumBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:manganeseIngot [com.hihellobyeoh.derptech.main .MainRegistry:init:63]:锰尘 [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:锰块 [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:ironDust [com.hihellobyeoh .derptech.main.MainRegistry:init:63]:cobaltIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:钴粉尘 [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:cobaltBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:nickelIngot [com.hihellobyeoh.derptech.main.MainRegistry:init :63]:nickelDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:nickelBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:copperIngot [com.hihellobyeoh.derptech.main .MainRegistry:init:63]:copperDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:copperBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:copperOre [com.hihellobyeoh .derptech.main.MainRegistry:init:63]:zincIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:zincDust [com.hihellobyeoh.derptech.main。MainRegistry:init:63]:zincBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:yttriumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:yttriumDust [com.hihellobyeoh。 derptech.main.MainRegistry:init:63]:yttriumBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:zirconiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:zirconiumDust [ com.hihellobyeoh.derptech.main.MainRegistry:init:63]:zirconiumBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:niobiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63] :niobiumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:niobiumBlock [com.hihellobyeoh.derptech.main。 MainRegistry:init:63]:molybdenumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:molybdenumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:molybdenumBlock [com.hihellobyeoh。 derptech.main.MainRegistry:初始化:63]:technetiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:初始化:63]:technetiumDust [com.hihellobyeoh.derptech.main.MainRegistry:初始化:63]:technetiumBlock [ com.hihellobyeoh.derptech.main.MainRegistry:init:63]:rutheniumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:rutheniumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63] :rutheniumBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:rhodiumIngot [com.hihell obyeoh.derptech.main.MainRegistry:init:63]:rhodiumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:rhodiumBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:palladiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:palladiumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:palladiumBlock [com.hihellobyeoh.derptech.main.MainRegistry:init: 63]:silverIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:silverDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:silverBlock [com.hihellobyeoh.derptech.main。 MainRegistry:init:63]:cadmiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:cadmiumDust [com.hihello byeoh.derptech.main.MainRegistry:init:63]:cadmiumBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:hafniumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:hafniumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:hafniumBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:tantalumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init: 63]:tantalumDust [com.hihellobyeoh.derptech.main.MainRegistry:初始化:63]:tantalumBlock [com.hihellobyeoh.derptech.main.MainRegistry:初始化:63]:tungstenIngot

等..... (我在那个文件中有113个变量atm)

这不是我想要的,我希望它,因为它运行getName函数,读取变量名称,然后将该变量名称写入字符串名称。现在用代码我给了我所有的变量,我在如何隔离我需要找到的确切变量的损失。

在此先感谢。

+3

对不起,我没有得到你所需要的。没有办法找到某个值被分配给的局部变量的名称,从你的解释我不明白你为什么需要这个。 – yole 2015-03-02 19:02:25

+0

你想要什么不被java支持。但是,它受C#支持。 http://www.codeproject.com/Articles/849265/What-s-new-in-Csharp-nameof-Expressions – 2015-03-02 19:27:20

回答

0

你只是想写它到一个变量?

Field[] fields = MainRegistry.class.getDeclaredFields(); 
//gives no of fields 
System.out.println(fields.length);   
for (Field field : fields) { 
    //gives the names of the fields 
    String name = field.getName().toLowerCase(); 
    if (name.indexOf("copper") > -1) { 
     System.out.println("you found copper: " + name); 
    } 
} 
1

如果我正确理解你的问题,而不是硬编码字符串,你希望返回小类的名字。 如何使用getClass().getName().toLowerCase()getName()方法CopperIngot.java

此外,您可以很好地使用反射来获取变量名称并设置新值。

Class aClass = MyObject.class; 
Field field = aClass.getField("field"); 
MyObject objectInstance = new MyObject(); 
field.set(objetInstance, value); 

希望这有助于

相关问题