开发者

this["var"+"name"] in java

开发者 https://www.devze.com 2022-12-29 01:15 出处:网络
In AS3 I can write this[\"foo\"] for access to a variable foo. I can construct any string in brackets. Is there a way to do this in开发者_如何学C Java?You can use Java\'s reflection API to achieve the

In AS3 I can write this["foo"] for access to a variable foo. I can construct any string in brackets. Is there a way to do this in开发者_如何学C Java?


You can use Java's reflection API to achieve the same effect, albeit much less elegantly. See here for a tutorial.


No, you can't do this. But you don't need to. There's an easier way to call variables. You just need to use this.foo to refer to the variable. Now, if you're trying to do something like

String var = "foo";
this[var] = "something else";

You may be able to do that with java reflection, but it would have quite a bit of overhead and I believe it would be quite inefficient.


No. If you want such kind of access, you should consider using Set interface (or reflection api, as noted before me).

0

精彩评论

暂无评论...
验证码 换一张
取 消