简体   繁体   English

Java - 使用变量引用对象

[英]Java - Referencing an object with a variable

I am trying to instantiate an object from within an if statement, basically an object when the user wants to create one.我试图从 if 语句中实例化一个对象,基本上是用户想要创建一个对象时的对象。 If the same code is being executed (with different arguments) then the reference to the object will be the same so…如果正在执行相同的代码(使用不同的参数),那么对对象的引用将是相同的,所以......

Can you reference the object from a different variable, for example a unique id that is created in the constructor for the object?您能否从不同的变量中引用该对象,例如在该对象的构造函数中创建的唯一 ID?

    Test test1 = null;
    if (run == true){
       test1 = new Test(argument1);
       test1 = test1.id;
       }

I am inexperienced, but basically want a variable from the object to then point at that object, so it does not get overwritten when a new object is instantiated from the code being executed again.我没有经验,但基本上想要一个来自对象的变量然后指向该对象,所以当一个新对象从再次执行的代码实例化时它不会被覆盖。

Thanks谢谢

I get the sense that you're overcompensating for a scenario which wouldn't normally occur in Java.我觉得您对 Java 中通常不会发生的场景进行了过度补偿。

To answer the question directly: you wouldn't be able to use the ID of an object to refer to the object later unless you had a data structure (like a Map<Integer, Test> ) keeping track of those instances.直接回答这个问题:除非您有一个数据结构(如Map<Integer, Test> )跟踪这些实例Map<Integer, Test>否则您以后将无法使用对象的 ID 来引用该对象。

In the scenario you illustrate, the only way that test1 is not null is if your test condition passes.在您说明的场景中, test1不为null的唯一方法是您的测试条件是否通过。 Outside of that, it stands a chance of being null and causing runtime issues later.除此之外,它有可能为null并在以后导致运行时问题。 Further, the reassignment to test1 would fail if test1.id is not also an instance of Test .此外,如果test1.id不是Test的实例,则重新分配给test1将失败。

You cannot dynamically name variables in Java.您不能在 Java 中动态命名变量。 May i suggest creating a List of Test variables?我可以建议创建一个Test变量List吗? Every time the loop executes, add the variable to the List .每次循环执行时,将变量添加到List This way you have a reference to all the objects that have been created.这样您就可以引用所有已创建的对象。

The instance won't be overwritten when you create a new instance, only your reference to the object will be overwritten.创建新实例时不会覆盖该实例,只会覆盖您对该对象的引用。 If you want to manage multiple instances of the object, you'll have to keep track of your references to them.如果要管理对象的多个实例,则必须跟踪对它们的引用。 You could keep an array of object references or collection of object references in your build function.您可以在构建函数中保留一组对象引用或一组对象引用。 Alternately, you could return the object reference to the caller and keep track of it there.或者,您可以将对象引用返回给调用者并在那里跟踪它。

You probably want to use something like an array here你可能想在这里使用类似数组的东西

int count = 0;
    Test[] tests = new Test[10];

    if(run == true){
        tests[count] = new Solution(argument1);
        count++;
    }

In this case we are making slots for 10 objects, really any number can go inside where that 10 is.在这种情况下,我们为 10 个对象制作插槽,实际上任何数字都可以放入 10 所在的位置。 It also might be a good idea to use something like an ArrayList if your not sure how many objects you need to make.如果您不确定需要制作多少个对象,使用 ArrayList 之类的东西也可能是个好主意。

To get an element from the array and use the object we can simply just use the name of the array followed by the spot in the array where it is located.要从数组中获取元素并使用对象,我们可以简单地使用数组的名称,后跟数组中它所在的位置。

tests[0].someMethod() //will call the method on the object at index 0

Remember arrays start counting at zero so if you want the 2nd element you have to ask for the element at index 1.记住数组从零开始计数,所以如果你想要第二个元素,你必须要求索引 1 处的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM