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

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

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:

  1. public class MyClass
  2. {
  3. private static int number1 = 1;
  4. private static int number2 = 2;
  5. public MyClass()
  6. {
  7. changeValueOf(number1, 3);
  8. }
  9. private static void changeValueOf(int number, int value)
  10. {
  11. //Change here the value of the correct field
  12. }
  13. }
英文:

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:

  1. public class MyClass
  2. {
  3. private static int number1 = 1;
  4. private static int number2 = 2;
  5. public MyClass()
  6. {
  7. changeValueOf(number1, 3);
  8. }
  9. private static void changeValueOf(int number, int value)
  10. {
  11. //Change here the value of the correct field
  12. }
  13. }

答案1

得分: 2

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

  1. private static void changeValueOf(int value)
  2. {
  3. number1 = value;
  4. }

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

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

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

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

  1. public class Example {
  2. public static void main(String[] args) {
  3. Example app = new Example();
  4. // 也可以是静态的
  5. Holder val1 = new Holder(1);
  6. Holder val2 = new Holder(2);
  7. app.changeValue(val1, 7);
  8. System.out.println(val1.value); // 7
  9. }
  10. public void changeValue(Holder holder, int newValue) {
  11. holder.value = newValue;
  12. }
  13. static class Holder {
  14. int value;
  15. Holder(int value) {
  16. this.value = value;
  17. }
  18. }
  19. }

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

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

  1. public class Example {
  2. private static int number1 = 2;
  3. private static int number2 = 3;
  4. public static void main(String[] args) {
  5. Example app = new Example();
  6. number1 = app.mul(number1, 7);
  7. number2 = app.mul(number2, 7);
  8. System.out.println(number1); // 14
  9. System.out.println(number2); // 21
  10. }
  11. public int mul(int a, int b) {
  12. return a * b;
  13. }
  14. }

希望这有所帮助!

英文:

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

  1. private static void changeValueOf(int value)
  2. {
  3. number1 = value;
  4. }

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:

  1. public class Example {
  2. public static void main(String[] args) {
  3. Example app = new Example();
  4. // Could be static as well
  5. Holder val1 = new Holder(1);
  6. Holder val2 = new Holder(2);
  7. app.changeValue(val1, 7);
  8. System.out.println(val1.value); // 7
  9. }
  10. public void changeValue(Holder holder, int newValue) {
  11. holder.value = newValue;
  12. }
  13. static class Holder {
  14. int value;
  15. Holder(int value) {
  16. this.value = value;
  17. }
  18. }
  19. }

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:

  1. public class Example {
  2. private static int number1 = 2;
  3. private static int number2 = 3;
  4. public static void main(String[] args) {
  5. Example app = new Example();
  6. number1 = app.mul(number1, 7);
  7. number2 = app.mul(number2, 7);
  8. System.out.println(number1); // 14
  9. System.out.println(number2); // 21
  10. }
  11. public int mul(int a, int b) {
  12. return a * b;
  13. }
  14. }

答案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.

  1. public class MyClass
  2. {
  3. private static int[] variables = {1, 2};
  4. public MyClass()
  5. {
  6. // change value of first variable
  7. changeValueOf(0, 3);
  8. // now variable[0] = 3
  9. }
  10. private static void changeValueOf(int number, int value)
  11. {
  12. variables[number] = value;
  13. }
  14. }
英文:

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.

  1. public class MyClass
  2. {
  3. private static int[] variables = {1, 2};
  4. public MyClass()
  5. {
  6. // change value of first variable
  7. changeValueOf(0, 3);
  8. // now variable[0] = 3
  9. }
  10. private static void changeValueOf(int number, int value)
  11. {
  12. variables[number] = value;
  13. }
  14. }

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:

确定