英文:
How to change the value of an instance variable of an object that didn't call the method?
问题
我正在用Java编写一个程序,我想在一个方法内部更改对象的实例变量的值,而该对象尚未调用该方法。我试图做类似于附加的代码。我想在myMethod方法内部更改object2.num的值,但会导致错误,因为object2超出了myMethod方法的范围。
public class Main {
public static void main(String[] args) {
Item object1 = new Item(2);
Item object2 = new Item(4);
object1.myMethod();
System.out.println(object1.num + " " + object2.num);
}
}
class Item {
int num;
Item(int n) {
num = n;
}
public int myMethod() {
object2.num *= this.num;
this.num *= object2.num;
}
}
我想到的唯一解决方案是给object1添加一个额外的实例变量num2,它将充当object2.num的角色。然后,在方法外部,我将更改object2.num为object1.num2。然而,这似乎非常不理想,并且在处理多个要更改的变量时会变得很麻烦。是否有更有效的方法来实现这一点?
我对编程还相对陌生,可能已经有解决方案了,所以非常感谢任何帮助或链接到解决方案的指引。
英文:
I'm writing a program in java in which I'd like to change the value of an instance variable of an object inside a method, while that object hasn't called the method. I'm trying to do something similar to the attached code. I'd like to change object2.num inside the method myMethod, but that will give an error, as object2 is outside the scope of myMethod.
public class Main {
public static void main(String[] args) {
Item object1 = new Item(2);
Item object2 = new Item(4);
object1.myMethod();
System.out.println(object1.num + " " + object2.num);
}
}
class Item {
int num;
Item(int n) {
num = n;
}
public int myMethod() {
object2.num *= this.num;
this.num *= object2.num;
}
}
The only solution I've come up with is to give object1 an additional instance variable, num2, which will act as object2.num. Outside of the method, I will then change object2.num to object1.num2. However, this seems very suboptimal and will get annoying when working with multiple variables to change. Is there any more efficient way to achieve this?
I'm still pretty new to coding and there might already be a solution to this, so any help or a link to a solution will be greatly appreciated.
答案1
得分: 0
> "... 我想在一个方法内部更改对象的实例变量的值,而该对象尚未调用该方法 ..."
从技术上讲,如果对象的变量是静态,你可以这样做。尽管如此,这似乎不符合您当前的设计逻辑。
> "... 我想在方法myMethod
内部更改object2.num
的值,但这将导致错误,因为object2
在myMethod
的范围之外 ..."
正确,在Java中,你无法在没有实际对象的情况下更改对象的变量。
> "... 我想到的唯一解决办法是给object1
添加一个额外的实例变量num2
,它将充当object2.num
。在方法之外,我将然后将object2.num
更改为object1.num2
..."
你需要提供有关任务的功能目的的更多详细信息。
我建议将object2
作为参数传递给myMethod
。此外,由于该方法返回一个int
,我在这里添加了一个return
语句。
public int myMethod(Item object2) {
object2.num *= this.num;
return this.num *= object2.num;
}
或者,你可以只声明方法的返回类型为void
。
public void myMethod(Item object2) {
object2.num *= this.num;
this.num *= object2.num;
}
然后,以object2
作为参数传递。
object1.myMethod(object2);
输出
16 8
英文:
> "... I'd like to change the value of an instance variable of an object inside a method, while that object hasn't called the method ..."
Technically you can, if the object's variable is static.
Although, this does not appear to be logical for your current design.
> "... I'd like to change object2.num inside the method myMethod, but that will give an error, as object2 is outside the scope of myMethod. ..."
Correct, in Java, you won't be able to change an object's variable without the actual object.
> "... The only solution I've come up with is to give object1 an additional instance variable, num2, which will act as object2.num. Outside of the method, I will then change object2.num to object1.num2. ..."
You'll have to provide more details about the functional purpose of the task.
I recommend passing object2 as a parameter to myMethod.
Additionally, I had to add a return here, since the method returns an int.
public int myMethod(Item object2) {
object2.num *= this.num;
return this.num *= object2.num;
}
Or, you could just declare the method with a void return type.
public void myMethod(Item object2) {
object2.num *= this.num;
this.num *= object2.num;
}
And then, supply object2 as the argument.
object1.myMethod(object2);
Output
16 8
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论