1

这是我的问题,我怎样才能在它的类之外更改一个对象,以便它保持在外部类中所做的更改?

下面是代码示例:

主类:

    public class Main {

        public static void main(String[] args)
        {
            Variable var = new Variable(1,2,3);
            Change.changeVar(var);
            System.out.println("" + var.geta() + "" + var.getb() + "" + var.getc());
        }
    }

变量类:

public class Variable {

private int a;
private int b;
private int c;

public Variable(int a, int b, int c)
{
    this.a = a;
    this.b = b;
    this.c = c;
}

public int geta()
{
    return this.a;
}

public int getb()
{
    return this.b;
}

public int getc()
{
    return this.c;
}

}

换班:

public class Change {

public static void changeVar(Variable var)
{
    Variable var2 = new Variable(4,5,6);
    var = var2;
}

}

4

6 回答 6

2
public class Variable {

    private int a;
    private int b;
    private int c;

    public Variable(int a, int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public int geta()
    {
        return this.a;
    }

    public int getb()
    {
        return this.b;
    }

    public int getc()
    {
        return this.c;
    }

    // depending on your use case, setters might be more appropriate
    // it depends on how you want to control the changing of the vars
    public void update(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

}

public class Change {

    public static void changeVar(Variable var)
    {
        var.update(4,5,6);
    }
}
于 2012-07-13T14:31:13.597 回答
2

在你的例子中,没有。当 changeVar() 退出时,参数 var 被丢弃,而 main() 方法中的 var 保留其原始值。通过参考阅读。

于 2012-07-13T14:26:55.953 回答
1

You cannot do it in a way that you described, because in Java variables are passed by values. However you can achieve the desired effect in a different way:

public class Variable {

    private int a;
    private int b;
    private int c;

    public Variable(int a, int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public int geta()
    {
        return this.a;
    }

    public int getb()
    {
        return this.b;
    }

    public int getc()
    {
        return this.c;
    }

    public void seta(int a) { this.a = a; }
    public void setb(int b) { this.a = b; }
    public void setc(int c) { this.a = c; }
}

public class Change {

    public static void changeVar(Variable var)
    {
        var.seta(4);
        var.setb(5);
        var.setc(6);
    }

}
于 2012-07-13T14:28:54.947 回答
0
public static void changeVar(Variable var)
{
    Variable var2 = new Variable(4,5,6);
    var = var2;
}

首先,你可以在Variable类中编写一些setter方法,然后你可以在上面的代码中调用这些setter方法,比如var.setA(4)...等等。enter code here

于 2012-07-13T14:32:21.843 回答
0

您需要提供 setter 方法并在原始对象上调用它们:

public void seta(int newa) { this.a = newa; }

然后你会说

public static void changeVar(Variable var)
{
    var.seta(4);
    //etc
}

您只是重新指向局部变量引用var以指向您的新实例var2。它对传递给方法的原始实例的值没有影响。

于 2012-07-13T14:27:06.137 回答
0

那样做?你不能。

您正在传递对实例的引用。但是,在函数内部,您使用新的引用。分配给新参考不会影响其他人。

于 2012-07-13T14:27:13.603 回答