返回值是什么意思? [英] What does it mean to return a value?

查看:376
本文介绍了返回值是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程很新,而且我对于返回一个值的确切含义感到困惑。起初,我认为这意味着要输出返回的值,但是当我在自己的代码中尝试时,没有任何反应。

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?

推荐答案

简单来说,它意味着将值返回给方法的调用者......

In simple terms, it means to return the value to caller of the method...

因此,在您的示例中,方法 getX 将返回 x 给调用者,允许他们访问它。

So, in your example, the method getX would return the value of x to the caller, allowing them access to it.

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...
    }
}

这篇关于返回值是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆