我对编程还很陌生,我对返回值的确切含义感到困惑。起初,我以为它意味着输出返回的值,但是当我在自己的代码中尝试时,什么也没有发生。
class Class1 {
public static int x = 3;
public static int getX(){
return x;
}
public static void main(String[] args){
Class1.getX();
}
}
这是我的意思的一个例子。当我运行该程序时,没有任何显示。考虑到这一点,我相信返回一个值意味着别的东西。但是什么?
最佳答案
简单来说就是将值返回给方法的调用者...
因此,在您的示例中,方法 getX
会将 x
的值返回给调用者,允许他们访问它。
class Class1{
static int x = 3;
public static int getX(){
return x;
}
public static void main(String args[]){
int myX = Class1.getX(); // return the value to the caller...
System.out.println(myX); // print the result to the console...
}
}
关于java - 返回一个值是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16312323/