简体   繁体   中英

Java: Setting a variable to a variable of a class, where the name of the class is a string?

This is my third (and hopefully final) question today.

Is it possible to declare the value of a variable to the variable of a class, where the name of the class is a string?

Example

int newalpha;
String color_name = "yellow";
Color red = new Color();
Color yellow = new Color();
Color blue = new Color();
newalpha = color_name.alpha

In this example, ".alpha" is a variable contained inside of the Color class. The part I'm wanting to simulate is the "newalpha = color_name.alpha", because obviously "color_name" is a String, not a color. Is this possible?

Thanks

TL;DR answer: no.

Reality: yes, sort of, via reflection, and ew.

Depending on your use case, there may be a way to achieve what you want, like by storing the colors in a hash map with the color names as the keys, so you'd just say colors.get(colorName).alpha .

Map<String, Color> colors = new HashMap<String, Color>() {{
    put("red", new Color());
    put("yellow", new Color());
    put("blue", new Color());
}};
String colorName = "yellow";
int newalpha = colors.get(colorName).alpha;

As posted elsewhere, the answer to your exact question is, "No." And my addition: "Use a more expressive language, like Lisp, Python, Ruby, or one like them."

I suspect that you're asking the wrong question though. What you really want is a way to get the alpha value of an arbitrary color. The object-oriented design advocate would use this as an opportunity to fill your head with factory methods, subtype polymorphism, enumerated types, and other such things.

Given my completely-uneducated guesstimate about your level of coding and the complexity of the project, it looks like an acceptable workaround (if not The Right Way) is a lot simpler:

int newAlpha = 0;
if(colorName.equals("yellow") newAlpha = yellow.getAlpha();
else if(colorName.equals("red") newAlpha = red.getAlpha();
else if(colorName.equals("blue") newAlpha = blue.getAlpha();
else //raise some kind of exception, assert false, or do something else that seems sensible

(Note that I've slightly changed a few names to match Java's style.)

I have no idea what's going on in the Color class (or even how red , yellow , and blue are different--with the current code they'll be the same thing (local variable names do not affect the object they refer to)), but it's probably appropriate to break the if-sequence into a separate method. And/or use a HashMap thus:

HashMap<String, Integer> alphaValues = new HashMap<String, Integer>();
alphaValues.put("red", /* something like red.getAlpha() */);
//etc.
// ...
newAlpha = alphaValues.get(colorName);

I haven't really addressed the scope issue--I'm sure you can figure it out.

Hope that helps!

Edit: Dave Newton beat me to the punch about the hashmap--he edited his post while I was creating mine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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