简体   繁体   English

使用字符串设置属性

[英]Setting properties using strings

Is it possible to set a property on an object using a string as the property name? 是否可以使用字符串作为属性名称在对象上设置属性? I am quite new to Java but in JavaScript I would just do something like this: 我对Java很陌生,但是在JavaScript中,我只会做这样的事情:

 Object[propertyNameAsString] = value

After searching around I found how to get a property using a string, but not how to set one. 搜索后,我发现如何使用字符串获取属性,但没有设置属性。 I also came across something called a Properties object, is that what I need to be using? 我还遇到了一个称为Properties对象的东西,那是我需要使用的东西吗?

Basically I just want to loop through an array filled with image names and then set their respective properties onto an object which I will use to store the actual images. 基本上,我只想遍历一个填充有图像名称的数组,然后将它们各自的属性设置到一个对象上,该对象将用于存储实际图像。 Is this possible in Java or should I do it another way? 这在Java中是可能的,还是我应该采用其他方式?

Edit: 编辑:

This is what my array looks like: 这是我的数组的样子:

 static String pngs[] = {"staminaBox", "staminaBoxR", "healthBox", "healthBoxR", "moveBox", "goButton"};

Ideally I want to loop through them and add properties to an assets class like so: 理想情况下,我想遍历它们并将属性添加到资产类中,如下所示:

 for (int i = 0; i < pngs.length; i++) {
    Assets.pngs[i] = createImageFromUrl(pngs[i] + ".png");      
 }

But now I'm realizing this wouldn't work because in my assets class I have to previously define the properties like: 但是现在我意识到这是行不通的,因为在资产类中,我必须事先定义以下属性:

 public static Image staminaBox;

And the whole point of me wanting to do this in the first place was so that I didn't have to write out declarations for every single image I wanted to add. 首先,我想做的所有事情是,这样我就不必为要添加的每个图像写声明。 I'm used to the loose nature of JavaScript, but does this mean there is no way of getting around these explicit declarations of properties? 我已经习惯了JavaScript的松散性质,但这是否意味着无法解决这些明确的属性声明? Will a HashMap let me accomplish this? HashMap可以让我做到这一点吗?

You might be talking about a couple different things. 您可能在谈论几件事。 Are you talking about setting the value of variables defined inside a class? 您是在谈论设置在类内部定义的变量的值吗? If so, you might be able to hack something together using reflection, but it's not a "gimme" in Java like it is in javascript. 如果是这样的话,您也许可以使用反射将某些东西合并在一起,但是它不是Java语言中的“笨拙”,就像JavaScript中那样。

You might also look into using a HashMap of properties to their values, or like you said, the Properties object (which is pretty much just a Map itself). 您可能还会考虑将属性的HashMap用作其值,或者像您所说的,使用Properties对象(几乎只是一个Map本身)。

You can use the Reflection API. 您可以使用Reflection API。 Keep in mind that it will get messy very quickly. 请记住,它将很快变得混乱。

Because Java uses the Getter and Setter pattern, you likely won't be assigning a value to a member variable but calling a method which itself assigns the value. 因为Java使用Getter和Setter模式,所以您可能不会为成员变量分配值,而是调用本身会分配值的方法。

How do I invoke a Java method when given the method name as a string? 当给定方法名称为字符串时,如何调用Java方法?

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

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