是否可以在Java中使用变量属性名称?(Is it possible to have variable attribute names in Java? If not, why?)

编程入门 行业动态 更新时间:2024-10-28 08:30:52
是否可以在Java中使用变量属性名称?(Is it possible to have variable attribute names in Java? If not, why?)

我想知道是否可以基于变量名创建属性。 更具体地说,请参阅以下示例。

for(int i = 0; i < 10; i++){ String number+i = i; }

这意味着每个属性的基本名称'number'都会被它的值i扩展。 所以我可以举例如下(假设这些将被存储和访问,并忽略属性/变量名称不应包含数字的事实,但你明白了):

System.out.println(number5); //You could do the same example with Strings and not numbers

显然第一部分没有编译,但我认为摘录清楚地说明了我的要求。 是否有可能的程序语言? 或者Java中有一个技巧可以实现这一目标吗?

然而,没有它的原因是什么? 显然,如果可能的话,属性可以存储两个变量值(属性的名称和值本身),这可能会导致混淆。 但这是没有它的唯一原因还是还有其他什么?

I'd like to know whether it is possible to create attributes based on variable names. To be more specific please see the following example.

for(int i = 0; i < 10; i++){ String number+i = i; }

That means every attribute would have the base name 'number' extended by it's value i. So I could for example do the following (assuming these would be stored and accessible, as well as ignoring the fact that attribute / variable names should not contain numbers, but you get the idea):

System.out.println(number5); //You could do the same example with Strings and not numbers

Obviously the first part does not compile but I think that excerpt makes clear what I'm asking about. Are there any program languages where this would be possible? Or is there a trick in Java to make this possible?

However what's the reason behind it not to have it? Obviously if this would be possible an attribute could store two variable values (the name of the attribute and the value itself) which might lead to confusion. But is this the only reason not to have it or is there anything else?

最满意答案

我认为简单的创建变量名称为int number+1 = 10, int number+2 = 20...是不可能的,因为如果你尝试将number+1值赋给另一个变量,它可能会太混乱。 看看这个: int another = number+1 。 在这种情况下,编译器不知道是否要查找具有名称number变量并将其递增或查找名称number+1变量。

但是反射为你提供了一些你想要实现的东西,但我建议只使用数组或Map,这些选项看起来更清晰,更简单,我认为它们为你提供了更多的使用反射的安全性。 无论如何,请看这个使用反射来动态使用字段名称的简单示例(请注意,在这种情况下,字段需要声明为public):

public class Test { public int field1 = 1; public int field2 = 2; public int field3 = 3; // this method changes values assigned to field1, field2 and field3: public void setFields() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { for (int i = 1; i < 4; i++) { Test.class.getField("field" + i).set(this, 25); // Here is what you may be looking for, you dynamically find field1, field2, ... } } public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { Test t = new Test(); for (int i = 1; i < 4; i++) { int j = (Test.class.getField("field" + i).getInt(t)); // Another example System.out.print(j + " "); } t.setFields(); for (int i = 1; i < 4; i++) { int j = (Test.class.getField("field" + i).getInt(t)); System.out.print(j + " "); } } }

执行此程序时得到的输出:

1 2 3 25 25 25

如您所见,当更改分配给field1 , field2和field3以及将它们打印到控制台时,我不必使用它们的名称,我可以使用循环并将String field与我从中得到的索引连接起来“for”循环。 无论如何,在声明这些变量时,我不可能以任何其他方式写出自己的名字,而不仅仅是输入field1, etc...

I think that simple creating variables names as int number+1 = 10, int number+2 = 20... is not possible because it could be too confusing if you try to assign value of number+1 to another variable. Look at this: int another = number+1 . In this case compiler wouldn't know if to look for variable with name number and to increment it or to look for variable with name number+1.

But reflection offers you a little bit of what you are trying to achieve, but I would suggest simply using arrays or Map, these option look more clear, more simple and I think that they offer you more safety that using reflection. Anyway, look at this simple example of using reflection to dynamically use names of fields (note that in this case field need to be declared as public):

public class Test { public int field1 = 1; public int field2 = 2; public int field3 = 3; // this method changes values assigned to field1, field2 and field3: public void setFields() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { for (int i = 1; i < 4; i++) { Test.class.getField("field" + i).set(this, 25); // Here is what you may be looking for, you dynamically find field1, field2, ... } } public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { Test t = new Test(); for (int i = 1; i < 4; i++) { int j = (Test.class.getField("field" + i).getInt(t)); // Another example System.out.print(j + " "); } t.setFields(); for (int i = 1; i < 4; i++) { int j = (Test.class.getField("field" + i).getInt(t)); System.out.print(j + " "); } } }

Output you get when executing this program:

1 2 3 25 25 25

As you see, when changing values assigned to field1, field2 and field3 and also when printing them to the console, I didn't have to use their names, I could use loop and concatenate String field with the index of i that I got from "for" loop. Anyway, when declaring these variables, I had no possibility to write their names any other way, than just typing field1, etc...

更多推荐

本文发布于:2023-08-04 16:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1418960.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   属性   名称   Java   attribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!