gpt4 book ai didi

Java:从其他类调用 String 返回方法返回 null

转载 作者:行者123 更新时间:2023-12-02 13:39:40 26 4
gpt4 key购买 nike

当我调用另一个类的方法时,它返回 null。我将在下面向您展示我的代码

class One{

Two two;

oneMethod(){
String str = "";

two.twoMethod(str);
S.o.p("Str in One class is " + str);



}


}


class Two{
String twoMethod(String str){
str = "From Two class";

S.o.p("Str in Two class is " + str);

return str;

}

}

输出如下:One 类中的 Str 为 ___//表示 null

二级中的Str是来自二级

我对此的理解是,在调用twoMethod()之后,最初为null的str现在将被新的str值“From Two class”覆盖。现在,当我们在 One 类中打印 str 时,它应该与在 Two 类中打印的内容相同。这就是我对流程的理解。

我是否错过了有关传递字符串的任何概念/规则?如有任何反馈,我们将不胜感激。谢谢!

最佳答案

你应该这样写:

class One{
Two two;
oneMethod(){
String str = "";

str = two.twoMethod(str);
S.o.p("Str in One class is " + str);
}
}

对于一类

关于Java:从其他类调用 String 返回方法返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42804110/

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