返回值是什么意思? - What does it mean to return a value? -开发者知识库
I'm fairly new to programming, and I'm confused about what it means exactly to return a value. At first, I thought it meant to output what value is being returned, but when I tried that in my own code, nothing happened.
我對編程很新,而且我對於返回一個值的確切含義感到困惑。起初,我認為這意味着要輸出返回的值,但是當我在自己的代碼中嘗試時,沒有任何反應。
class Class1 { public static int x = 3; public static int getX(){ return x; } public static void main(String[] args){ Class1.getX(); }}
This is an example of what I mean. When I run the program, nothing shows up. Considering this, I'm led to believe returning a value means something else. But what?
這是我的意思的一個例子。當我運行程序時,沒有任何顯示。考慮到這一點,我被引導相信返回一個值意味着別的東西。但是什么?
3 个解决方案
#1
5
In simple terms, it means to return the value to caller of the method...
簡單來說,它意味着將值返回給方法的調用者......
So, in your example, the method getX
would return the value of x
to the caller, allowing them access to it.
因此,在您的示例中,方法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... }}
最佳答案: