动态Java变量命名 [英] Dynamic Java Variable Naming

查看:164
本文介绍了动态Java变量命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题比什么都更能促进我的知识...

This question is more for furthering my knowledge than anything...

Java是否具有类似于PHP生成变量名的功能?我有一个SCJA证书,我正在为SCJP学习,但从未见过,但是很好奇。

Does Java have anything similar to PHP's ability to generate a variable name? I have an SCJA Cert and I'm studying for the SCJP and have never seen this, but was curious.

PHP示例

$application->{$request->getParameter("methodCall")}($request->getParameter('value'));

Java是否有类似的东西?我已经在这里阅读了,一般的答案是使用我不感兴趣的HashMap,因为这不是解决实际问题的方法。我对这可能的解决方案更感兴趣?

Does Java have anything similar? I've been reading on here and the general answer is to use a HashMap which I'm not interested in since this isn't to solve a real problem. I'm more interested in the is this possible solution? If not so be it, but just trying to expand my knowledge!

谢谢,
Jared

Thanks, Jared

推荐答案

否,变量(字段和局部变量)是在Java编译时静态创建的。

No, variables (fields and local variables) are statically "created" at compile-time in Java.

当然,内存仅在运行时被占用,但是对象的多少个字段由编译时决定。

Of course memory is only ever occupied at runtime, but how many and which fields an object has is decided at compile-time.

因此,您不能在其中动态添加字段 Java。

Therefore you can't "dynamically add a field" in Java.

是的:地图 是该问题的解决方案。 添加字段通常不是问题,而是尝试的解决方案,它适用于某些语言(通常是动态语言),而不适用于其他语言。

And yes: A Map is the solution to the problem. "Adding a field" is not usually the problem but an attempted solution that's appropriate for some languages (usually dynamic ones) and inappropriate for others.

这篇关于动态Java变量命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆