简体   繁体   English

魔术将变量传递给Java中的参数

[英]Magic passing variable to parameter in Java

public class experiment3 {
    private static void mystery(String foo, String bar, String zazz) {
        System.out.println(zazz + " and " + foo + " like " + bar);
    }
    public static void main(String[] args) {
        String foo = "peanuts";
        String bar = "foo";
        mystery(bar, foo, "John");
    }
}

Can somebody explain to me how this result is formed when outputting it? 有人可以向我解释一下输出结果如何形成吗?

The output will be: 输出将是:

John and foo like peanuts 约翰和福如花生

I understand that param. 我了解该参数。 name zazz always is John; 扎兹这个名字总是约翰。

I don't understand how the last 2 params. 我不明白最后两个参数。 were formed?! 形成了?

PS: Please help me to understand how this 2 last params were formed. PS:请帮助我了解这最后两个参数是如何形成的。 If there is a possibility for a schematic representation for better understanding the way that java compiler works! 如果有可能用示意图表示来更好地理解java编译器的工作方式!

The Java compiler doesn't care about the names of the variables passed in as arguments of a method call as it pertains to the names of the parameters of the method that is called. Java编译器不关心作为方法调用的参数传入的变量的名称,因为它与所调用方法的参数的名称有关。 Only the position of the values matters. 仅值的位置很重要。

                             "foo"        "peanuts"
                               |               |   
                               v               v       
                    mystery(   bar    ,       foo , "John")
                               |               |       |
                               v               v       v
private static void mystery(String foo, String bar, String zazz)

The mixed order of variable names here serves no purpose here except to confuse. 此处变量名称的混合顺序在这里没有任何用途,除非造成混淆。

zazz + " and " + foo + " like " + bar

becomes

John and foo like peanuts 约翰和福如花生

When you're making the call to the 'mystery' method you give the params : 当您调用“神秘”方法时,您将给出以下参数:

bar(="foo"), foo(="peanuts), "John".

the names of these variables have nothing to to with the way you declare the method, only their content, so, the method receives the params: 这些变量的名称与声明方法的方式无关,仅与它们的内容无关,因此,该方法将接收以下参数:

 foo(="foo"), bar(="peanuts"), zazz(="John")

I do not see any mystery in this code: you must follow the order of the arguments in the signature of your method and the order of the arguments to the call of this method. 我在此代码中看不到任何神秘之处:您必须遵循方法签名中参数的顺序以及此方法调用的参数顺序。

If the same order had been respected in both directions with the same names of variables foo, bar and zazz; 如果在两个方向上都遵循相同的顺序,并且使用了相同的变量名foo,bar和zazz; the output to the display would have been simply: 显示器的输出将很简单:

"john and peanuts like foo".

But because the order has been inverted, it is necessary to follow the position of each variable in the signature of the method to know the value that will be returned. 但是,由于顺序已经颠倒,因此必须遵循方法签名中每个变量的位置,才能知道将要返回的值。 So in the signature on: 所以在签名上:

foo = 1, bar = 2 and zazz = 3

But in the call we have: 但是在通话中,我们有:

bar = 1 and its value = foo
foo = 2 and its value = peanuts

the value of zazz = john from where the display 从哪里显示zazz = john的值

"john and foo like peanuts"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM