English 中文(简体)
Java:使用扼杀手段进行静态接触?
原标题:Java: Static access using a string?

在标题上太紧了,但我有一份用于检索某些文本坐标的文件。 档案被称作资产,只是包含一堆诸如以下的物品。

public static TextureRegion level1;

我只使用资产。 方案另一部分及各部分。

What I m wondering is if there is a way to do this through a string, for example instead of Assets.level2 i could do something like Assets.(string) where string = "level2"

任何帮助都将是巨大的。

最佳回答

(TextureRegion)Assets.class. Field(“level2”)get(null)

问题回答

您不应在<条码>Asets类别中将这些领域作为静态领域,而应在<条码>中添加固定方法:

public static TextureRegion getTextureRegion(String name)
{
    // get it somehow
}

现在,对于“some”部分而言:最容易(和最灵活)的方式是,在包含案文区域的<条码>-条码>类别中,有<条码>。 你们如何把数据列入这一地图。 例如:

regions.put("level1", your_level_1_region);

之后,getTextureRegion成为:

public static TextureRegion getTextureRegion(String name)
{
    return regions.get(name);
}

接下来是,这些区域可以在运行时间(可能从档案中装载)而不是硬编码。

你们可以通过使用 Java反省文稿进入一个基于变量的班级的固定田地。

TextureRegion lvl = (TextureRegion) Assets.class.getDeclaredField("level1").get(null);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签