gpt4 book ai didi

Java 按引用传递与按值传递的麻烦

转载 作者:行者123 更新时间:2023-12-01 15:43:56 25 4
gpt4 key购买 nike

为什么我在传递引用示例中遇到错误 obj1.add200really 带有下划线

public class Test {

private int number;

Test(){
number = 1;
}

public static void main(String[] args) {
Test obj1 = new Test();
System.out.println("the number is " + obj1.number);
System.out.println("the number 1 plus 200 is " + obj1.add200(obj1.number));
System.out.println("while the number is still " + obj1.number);
System.out.println("\n");
System.out.println("the number is " + obj1.number);
System.out.println("the number 1 plus 200 is " + obj1.add200really(obj1.number));
System.out.println("while the number is still " + obj1.number);
}


int add200(int somenumber){
somenumber = somenumber + 200;
return somenumber;
}
int add200really(Test myobj){
myobj.number = 999;
return myobj.number;
}
}

最佳答案

使用obj1.add200really(obj1);

关于Java 按引用传递与按值传递的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7509521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com