简体   繁体   English

如何使用名称来访问Java中的变量?

[英]How to use names to access variables in Java?

I am trying to write a method in a class which could be invoked several times, each time modifying one of the class' fields. 我试图在类中编写一个方法,可以多次调用,每次修改一个类的字段。 However, I need to new the object and set the field's value to it if I want to modify it, but if do this inside the method, the reference seem to be lost and the field left unchanged after the calling. 但是,如果我想修改它,我需要new对象并将字段的值设置为它,但如果在方法内执行此操作,则引用似乎丢失并且字段在调用后保持不变。

Public class A {
    private Immutable a;  //Immutable class
    private Immutable b;
    public void modify(Immutable k,int v) {
        k=new Immutable(v);  //Now I am trying to pass 
                             //a and b as parameters but they remain unchanged
    }
}

Is there any way to pass the name of the field into the method (eg, change the method modify(Immutable k, int v) to modify(String kName, int v) , then use the name of the field to access it? 有没有办法将字段的名称传递给方法(例如,更改方法modify(Immutable k, int v)modify(String kName, int v) ,然后使用字段的名称来访问它?

Thanks for any inputs! 感谢您的任何投入!

If you need to access a variable by a String key, you should use a Map . 如果需要通过String键访问变量,则应使用Map

Map<String, Immutable> _vars = new HashMap<String, Immutable>();

public void modify(String key, int v) {
  _vars.put(key, new Immutable(v);
}

What I understand is that you're trying to create a new Immutable given an integer (v). 我的理解是你试图在给定整数(v)的情况下创建一个新的Immutable。 In your modify method right now, k is a temporary reference. 在你的修改方法中,k是一个临时引用。 Setting the value "k =" in here, only affects the reference stored here in this method, not whatever reference you called modify with. 在此处设置值“k =”仅影响此方法中存储的引用,而不影响您调用的引用。

You have client code like this currently: 您目前有这样的客户端代码:

A a = new A();
Immutable k = new Immutable(x);
a.modify(k, y);

and you're hoping that k will be changed. 而且你希望k会被改变。 What you really want instead of the 3rd line is: 你真正想要的不是第3行:

k = new Immutable(y);

Assuming that things are really more complicated, then I would need more information to help you further. 假设事情真的更复杂,那么我需要更多信息来帮助你。

Java does not support Call-by-name or call-by-reference, only Call-by-value. Java不支持Call-by-name或call-by-reference,只支持Call-by-value。 Your k variable (the method parameter) is completely independent from any variable used outside of the class (if there was one at all). 你的k变量(方法参数)完全独立于类之外使用的任何变量(如果有的话)。

You could use Reflection to support passing "a" or "b" (or a Field object), but you should not. 您可以使用Reflection支持传递“a”或“b”(或Field对象),但您不应该这样做。

Better have two methods: 最好有两种方法:

public void setA(int v) {
    this.a = new Immutable(v);
}

public void setB(int v) {
    this.b = new Immutable(v);
}

If it is more complicated than a single constructor call, factor the common part out to a common method. 如果它比单个构造函数调用更复杂,则将公共部分考虑为常用方法。

Use a value holder. 使用价值持有人。

public class ValueHolder<T> {
    private T value ;
    public ValueHolder(T value) {
        this.value = value ;
    }
    public T get() { 
        return value ; 
    }
    public void set(T value) { 
        this.value = value; 
    }
    public static <V> ValueHolder<V> make(V value) {
        return new ValueHolder<V>(value);
    }
}
public class Processor {
    private Inmutable a ;
    private Inmutable b ;
    public void modify(ValueHolder<Inmutable> k, int v) {
        k.set(new Inmutable(v));
    }
}

Once that is done you can get the instance you just created from the value holder. 完成后,您可以从价值持有者处获得刚刚创建的实例。

Processor processor = new Processor();
ValueHolder<Inmutable> holder = ValueHolder.make(k);
processor.modify(holder, value);
k = holder.get() ;

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

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