是否可以实例化具有变量名的对象,或在运行时访问变量名? [英] Is it possible to instantiate objects with variable names, or access variable names at runtime?

查看:56
本文介绍了是否可以实例化具有变量名的对象,或在运行时访问变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多行代码创建对象,并使用具有相似对象名称和构造函数的各种参数.唯一发生变化的是正在创建的对象变量的实际名称以及传入的对象本身的名称.这是与我当前设置匹配的代码示例:

I have quite a few lines of code that create objects and using various parameters with similar object names and constructors. The only thing that is changing is the actual name of the object variable being created, and the name of the objects themselves being passed in. Here is an example of code that matches my current setup:

BackyardObject backyardObject0 = new BackyardObject(cat0, dog0, goat0, piglet0);
BackyardObject backyardObject1 = new BackyardObject(cat1, dog1, goat1, piglet1);
BackyardObject backyardObject2 = new BackyardObject(cat2, dog2, goat2, piglet2);
BackyardObject backyardObject3 = new BackyardObject(cat3, dog3, goat3, piglet3);
BackyardObject backyardObject4 = new BackyardObject(cat4, dog4, goat4, piglet4);
// many many more BackyardObjects being instantiated

我们可以看到,正在创建的对象的名称与传递给构造函数的对象的名称完全匹配.是否可以创建一个循环来设置所有这些内容?

As we can see, the names of the object being created match exactly the names of the objects being passed into the constructor. Is it possible to create a loop that would set all this up?

编辑

我认为我可能缺少获得此问题正确答案所需的详细信息.这不是如何"使用循环或如何向集合中添加项目的问题,更多的问题是确定是否有可能在访问时在循环内部动态创建变量名"在循环内部动态提供的另一个变量名称提供了上面给出的信息(只是当场编写的代码).

I think I might have lacked details that were needed to get a correct answer for this question. It isn't a question on "how-to" use a loop, or how to add items to a collection it's more of a question to determine if is it possible to create a "variable name" dynamically inside of a loop, while accessing another variable name dynamically inside of the loop provided the information given above (just made up code on the spot).

// psuedo code for something I'm asking is possible
for( i = 0; i < 10; i++)
{
    // create BackyardObject with generic name, while appending "i" to 
    // variable name, and accessing other object variables
    BackyardObject backyardObject'i' 
        = new backyardObject(cat'i', dog'i', goat'i', piglet'i');
}

虽然我知道我可以创建其他数组和列表来存储对象,然后使用它们,但我只是在查看是否有可能动态获取变量名.我不确定是否完全可能,这就是为什么我问这个问题.我知道这是一个奇怪的问题,但是在我看到此Objective-C代码后感到好奇:

While I understand that I could create additional arrays and lists to store objects and then use those, I was just seeing if it was possible to get variable names dynamically. I wasn't sure if it was entirely possible, that's why I asked the question. I know that this is a strange question, but got curious after I seen this Objective-C code:

// Getting an arrayName dynamically based on loop index
for (int i = 0; i < 10; i++)
{
    NSString *arrayName = [[NSString alloc] initWithFormat:@"column%d",
        i];
    NSArray *array = [self valueForKey:arrayName];
}

推荐答案

对象没有名称...您所说的是变量.而且,与其使用大量带有数字作为后缀的变量,不如使用 collections -数组,列表或其他方法,会更好.然后,您可以执行以下操作:

Objects don't have names... what you're talking about is variables. And rather than having lots of variables with numbers as suffixes, you'd be better using collections - arrays, or lists, or whatever. Then you can do:

// Or use arrays...
List<Cat> cats = new List<Cat>();
cats.Add(new Cat(...)); // Add the cats however you want to set them up
// Ditto dogs, goats etc

List<BackyardObject> backyardObjects = new List<BackyardObject>();
for (int i = 0; i < 10; i++)
{
    backyardObjects.Add(new BackyardObject(cats[i], dogs[i],
                                           goats[i], piglets[i]));
}

我假设您是C#的新手,我建议您从所学的内容中查看数组和集合.

I assume you're fairly new to C# - I suggest you look at arrays and collections in whatever you're learning from.

这篇关于是否可以实例化具有变量名的对象,或在运行时访问变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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