改变函数参数的值? [英] change a functions argument's values?

查看:97
本文介绍了改变函数参数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能看起来像一个愚蠢的问题,但是这个函数实际上会影响变量 bool (我有更多的上下文来使用它,但是这个基本上我不确定)? (我正在询问关于java的具体内容)

  void truifier(boolean bool){
if(bool == false) {
bool = true;
}
}


解决方案

一个稍微不同的例子:

  public class Test {

public static void main(String [] args ){
boolean in = false;
truifier(in);
System.out.println(in is+ in);


public static void truble(boolean bool){
if(bool == false){
bool = true;
}
System.out.println(bool is+ bool);




$ b

运行这个程序的输出是: p>

  bool是真的
in是


$ b bool 变量将更改为true,但只要结构符方法返回,那个参数变量就会消失(当人们说它超出了范围时,这就是人们的意思)。然而,传入到结构符方法的变量中的保持不变。


This may seem like a stupid question, but would this function actually affect the variable bool (there is greater context to how I'm going to use this, but this is basically what I'm unsure about)? (I am asking specifically about java)

void truifier (boolean bool) {
    if (bool == false) {
        bool = true;
    }
}

解决方案

Consider a slightly different example:

public class Test {

    public static void main(String[] args) {
        boolean in = false;
        truifier(in);
        System.out.println("in is " + in);
    }

    public static void truifier (boolean bool) {
        if (bool == false) {
            bool = true;
        }
        System.out.println("bool is " + bool);
    }
}

The output from running this program would be:

bool is true
in is false

The bool variable would be changed to true, but as soon as the truifier method returned, that argument variable goes away (this is what people mean when they say that it "falls out of scope"). The in variable that was passed in to the truifier method, however, remains unchanged.

这篇关于改变函数参数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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