在Java中,是否可以修改作为函数参数传递的属性值?

huangapple go评论50阅读模式
英文:

Is it possible to modify the value of an attribute sent in argument of a function in Java?

问题

I'm working on a calculator and I search how I can optimize my code.
The thing is that I have much code duplication due to if I'm working on the first number of the calculation or the second.
So I'm searching if it is possible to modify the value of an attribute sent in argument of a function? (I think not because I saw nowhere the answer).

Maybe I'm expressing myself badly so here is a code below to explain what I'm talking about:

public class MyClass
{
    private static int number1 = 1;
    private static int number2 = 2;

    public MyClass()
    {
        changeValueOf(number1, 3);
    }

    private static void changeValueOf(int number, int value)
    {
        //Change here the value of the correct field
    }
}
英文:

I'm working on a calculator and I search how I can optimize my code.<br/>
The thing is that I have much code duplication due to if I'm working on the first number of the calculation or the second. <br/>So I'm searching if it is possible to modify the value of an attribute sent in argument of a function ? (I think not because I saw nowhere the answer).
<br/><br/>Maybe I'm expressing myself badly so here is a code below to explain what I'm talking about:

public class MyClass
{
    private static int number1 = 1;
    private static int number2 = 2;

    public MyClass()
    {
        changeValueOf(number1, 3);
    }

    private static void changeValueOf(int number, int value)
    {
        //Change here the value of the correct field
    }

}

答案1

得分: 2

首先,您可以在方法内修改静态变量:

private static void changeValueOf(int value)
{
    number1 = value;
}

但我猜这不是您正在寻找的 在Java中,是否可以修改作为函数参数传递的属性值?

在Java(以及大多数其他语言中),原始数据类型(例如intshortlong等)按值传递,即将值的副本传递给方法(函数)。
而引用类型(对象,例如使用new运算符创建的对象)按引用传递。因此,当您修改引用类型(对象)的值时,您可以在外部范围(例如,在方法调用者中)看到更改。

所以答案是不行 - 您无法更改int的值以便外部范围能看到更新后的值。

不过,您可以将您的int值包装在某个对象中 - 然后在其中更改该值:

public class Example {
    public static void main(String[] args) {
        Example app = new Example();

        // 也可以是静态的
        Holder val1 = new Holder(1);
        Holder val2 = new Holder(2);

        app.changeValue(val1, 7);

        System.out.println(val1.value); // 7
    }

    public void changeValue(Holder holder, int newValue) {
        holder.value = newValue;
    }

    static class Holder {
        int value;
        Holder(int value) {
            this.value = value;
        }
    }
}

此外,您还可以创建一个包含两个值的数组,并在方法内部更新它们,但这不是一个很好的方法,我个人认为。

最后,您可以只返回更新后的值并将其分配给您的变量:

public class Example {
    private static int number1 = 2;
    private static int number2 = 3;

    public static void main(String[] args) {
        Example app = new Example();

        number1 = app.mul(number1, 7);
        number2 = app.mul(number2, 7);

        System.out.println(number1); // 14
        System.out.println(number2); // 21
    }

    public int mul(int a, int b) {
        return a * b;
    }
}

希望这有所帮助!

英文:

First of all, you can modify static variables inside the method:

private static void changeValueOf(int value)
{
    number1 = value;
}

But I guess that is not what you a looking for 在Java中,是否可以修改作为函数参数传递的属性值?

In Java (and in most other languages) primitive data type (int, short, long, etc) passed by value, e.g. the copy of value passes to the method (function).
And reference types (objects, e.g. created with new operator) passed by reference. So, when you modigy the value of reference type (object) you can see the changes in the outer scopes (for example, in method caller).

So, the answer is no - you cannot change the value of int so that the outer scope would see the updated value.

Howewer, you could wrap your int values with some object - and it change the value inside of it:

public class Example {
    public static void main(String[] args) {
        Example app = new Example();

        // Could be static as well
        Holder val1 = new Holder(1);
        Holder val2 = new Holder(2);

        app.changeValue(val1, 7);

        System.out.println(val1.value); // 7
    }

    public void changeValue(Holder holder, int newValue) {
        holder.value = newValue;
    }

    static class Holder {
        int value;
        Holder(int value) {
            this.value = value;
        }
    }
}

Also, you could create an array with 2 values and update them inside the method, but it's not very good approach IMO

And finally, you could just return updated value and assign it to your variables:

public class Example {
    private static int number1 = 2;
    private static int number2 = 3;

    public static void main(String[] args) {
        Example app = new Example();
        
        number1 = app.mul(number1, 7);
        number2 = app.mul(number2, 7);

        System.out.println(number1); // 14
        System.out.println(number2); // 21
    }

    public int mul(int a, int b) {
        return a * b;
    }

}

答案2

得分: 1

One possibility is to use an array to store your variables, instead of separate variables with numbers affixed. Then you would write number[1] instead of number1 for example. You can pass the array index number around to indicate which variable you are referring to.

public class MyClass
{
    private static int[] variables = {1, 2};

    public MyClass()
    {
        // change value of first variable
        changeValueOf(0, 3);

        // now variable[0] = 3
    }

    private static void changeValueOf(int number, int value)
    {
        variables[number] = value;
    }

}
英文:

One possibility is to use an array to store your variables, instead of separate variables with numbers affixed. Then you would write number[1] instead of number1 for example. You can pass the array index number around to indicate which variable you are referring to.

public class MyClass
{
    private static int[] variables = {1, 2};

    public MyClass()
    {
        // change value of first variable
        changeValueOf(0, 3);

        // now variable[0] = 3
    }

    private static void changeValueOf(int number, int value)
    {
        variables[number] = value;
    }

}

huangapple
  • 本文由 发表于 2020年8月1日 23:24:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63206890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定