如何用变量定义java对象名? [英] How to define a java object name with a variable?

查看:163
本文介绍了如何用变量定义java对象名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用通过循环轻松获得的命名模式来创建大量对象。是否有任何方法可以从变量中读取对象名称,如下所示?

I need to create a large number of objects using a pattern of naming easily obtainable through a loop. Is there any way to have an object name be read from a variable, like this?

String Var = "ObjectName";
ClassName Var = new ClassName();

我不需要为对象分配变量名,只是为了从中获取名称在分配时。

I don't need the object to be assigned a variable name, merely to obtain the name from it at the time of assignment.

推荐答案

您选择的单词表明您并不完全了解变量和对象的工作方式,并且你需要修复它以获得Java的任何地方。

Your choice of words shows that you don't fully understand the way variables and objects work, and you need to fix that to get anywhere with Java.

如果我写:

Item myItem = new Item();

我创建了一个新对象,我定义了一个指向的变量那个对象。

I create a new object, and I define a variable which points to that object.

对象没有名称(它有一个ID,这是在运行时分配的数字;大多数程序员可以忽略它)。

The object does not have a name (it has an ID, which is a number assigned at runtime; most programmers can ignore it).

该变量的名称为 myItem 。变量指向对象。稍后,它可能指向另一个对象。变量可能超出范围并且不再存在,而对象仍然存在。

The variable has a name, myItem. The variable points to the object. Later on, it might point to a different object. The variable might fall out of scope and cease to exist, while the object continues to exist.

你可能会说,这并不重要。如果我说'的名字对象',我的意思是'指向对象的变量的名称'。

You might say, "that's not important. If I say the 'name of the object', I mean 'the name of the variable pointing to the object'."

但这很重要,因为很多对象都会指向超过一个变量:

But it is important, because lots of objects will be pointed to by more than one variable:

Item myItem = new Item();
Item yourItem = myItem;
// both variables point to the same object. Neither variable is "more" the
// owner of the object than the other.

...许多对象不会直接由变量指向。

... and lots of objects won't be pointed to directly by a variable.

myList.append(new Item()); 
// you can't get at this new object using a direct variable.






在Java(和大多数主流语言)中,你无法在运行时创建变量名称。 唯一存在的变量名称,实际上是源代码中的变量名称

所以没有任何效果如下:

So there's nothing that works like this:

int number = 1;
Cell cell$number = new Cell(); // can't be done!
Cell currentCell = cell1; 

您也无法根据运行时数据按名称访问变量。所以没有什么能像这样工作:

Nor can you access variables by name, based on runtime data. So there's nothing that works like this:

Cell cell1 = ...;
int number = 1;
Cell currentCell = cell$number; // can't be done!

这是一件好事,因为它可以让编译器验证有关代码的某些事情。

This is a good thing because it lets the compiler validate certain things about your code.

在某些语言中你可以用eval()来实现这样的东西 - 但是你应该跑一英里!

In some languages you could possibly achieve something like this using eval() -- but you should run a mile from it!

这是一个提示,如果您正在编写如下代码:

This is a hint that if you're writing code like:

 Cell cell0 = new Cell(...);
 Cell cell1 = new Cell(...);
 ...
 Cell cell99 = new Cell(...);

...然后你做错了什么。它可能会起作用,但它会严重缩放。没有办法将它放入循环中。 (别担心 - 当我们开始编程时,我们大多数人都会遇到这个问题。)

... then you're doing something wrong. It may work, but it scales badly. There is no way to put this into a loop. (Don't worry - most of us hit this issue when we began programming).

如果你使用的是数组:

 Cell[] cells = new Cell[];
 cells[0] = new Cell(...);
 cells[1] = new Cell(...);
 ...
 cells[99] = new Cell(...);

然后你可以改用一个循环:

Then you could use a loop instead:

 for(int i = 0; i<100; i++) {
     cells[i] = new Cell(...);
 } 

数组是最简单的集合对象。还有集合,列表,地图和一系列更高级的集合,以满足大多数需求。

An array is the simplest of "collection" objects. There are also Sets, Lists, Maps, and a host of more advanced collections, to suit most needs.

您似乎想要使用字符串存储和访问对象作为键。这样做的方法是使用Map。

You seem to want to store and access objects using strings as a key. The way to do this is using a Map.

Map<String,Cell> myMap = new TreeMap<String,Cell>();

myMap.put("AA", new Cell(...));
myMap.put("AB", new Cell(...));

...

Cell currentCell = myMap.get("AA");

地图是一个界面的。 TreeMap 只是一个提供 Map 接口实现的类。阅读标准JRE提供的 Map 的各种实现。

Map is an interface. TreeMap is just one class that provides an implementation of the Map interface. Read up on the various implementations of Map provided by the standard JRE.

密钥不必是 String 。任何实现 equals()的东西都可以用作密钥。

The key doesn't have to be a String. Anything that implements equals() can be used as a key.

使用:

for(char c = 'A'; c <= 'L'; c++) {
   for(char d = 'A'; d<= 'L'; d++) {
       myMap.put("" + c + d, new Cell(...));
   }
}

但是,因为你说你想要一个网格,最好使用2D数组,并在需要时将数字坐标转换为字母网格引用。谷歌的2D阵列Java,你会发现很多例子。

However, since you've said you want a grid, it's probably better to work with a 2D array, and translate the numeric coordinates to and from a lettered grid-reference whenever you need to. Google for "2D array Java", and you'll find plenty of examples.

Cell[][] cells = new Cell[12][12];
for(int x=0; x<12; x++) {
   for(int y=0; y<12; y++) {
       Cell cell = new Cell();
       cell.setName( "" + ('A' + x) + ('A' + y)); // perhaps
       cells[x][y] = cell;
   }
}

除了标准的JRE之外,还有很多其他实现地图。例如,Spring提供了DefaultRedisMap,其中对象存储在Redis数据库中。

Beyond the standard JRE, there are plenty of other implementations of Map. For example, Spring provides DefaultRedisMap, in which the objects are stored in a Redis database.

更一般地说 - 你在这里问了一个非常基本的问题。您应该阅读任何体面的Java书籍中 Collections API 的章节。

More generally - you have asked a very basic question here. You should read the chapter on the Collections API in any decent Java book.

这篇关于如何用变量定义java对象名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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