Java 中的形参是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27494316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:53:07  来源:igfitidea点击:

What is a formal parameter, in Java?

javaclassparameters

提问by bywronski

I'm currently working on java legacy code and I encounter with a class that represent a formal parameter but I don't know why is that. I read about C++ Formal Parameters, but it confused me because in C++ it is the same as the argument (I'm in doubt about this affirmation) and in my legacy code it is a class, that has only a private int member that store a number (with their set & get methods) but honestly, I didn't find the whyof that declaration.

我目前正在处理 Java 遗留代码,我遇到了一个表示形式参数的类,但我不知道为什么会这样。我读过C++ Formal Parameters,但它让我感到困惑,因为在 C++ 中它与参数相同(我对这个肯定表示怀疑)并且在我的遗留代码中它是一个类,它只有一个私有 int 成员存储一个数字(带有它们的 set 和 get 方法)但老实说,我没有找到声明的原因

回答by Elliott Frisch

In Java and in C++ the formal parameter is specified in the signature of the method:

在 Java 和 C++ 中,形式参数在方法的签名中指定:

public void callIt(String a)

callIthas a single formal parameter that is a String. At run-time we talk about actual parameters (or arguments), the :

callIt有一个形式参数 a String。在运行时我们谈论实际参数(或参数),即:

callIt("Hello, World");

"Hello, World" Stringis an actual parameter, String ais a formal parameter.

“Hello, World”String是实参,String a是形参。

From the Wikipedia entry for parameter:

从维基百科条目的参数

The term parameter(sometimes called formal parameter) is often used to refer to the variable as found in the function definition,

术语参数(有时称为形式参数)通常用于指代函数定义中的变量,

and:

和:

argument(sometimes called actual parameter) refers to the actual input passed.

参数(有时称为实参)是指传递的实际输入。

回答by Solomon Slow

Not disagreeing with Elliot Frisch at all, but I can say it more simply:

完全不反对 Elliot Frisch,但我可以更简单地说:

The variable wis a "formal parameter" in the following function definition:

该变量w是以下函数定义中的“形式参数”:

void foobar(Widget w) {
    ...
}

The value returned by nextWidget(...)is the "actual parameter" when you write the following function call:

nextWidget(...)编写以下函数调用时,返回的值是“实际参数” :

foobar(nextWidget(...));

回答by NAGHMAAN MOHASEEN

Well, coming to Java or any language these definitions always hold true:

好吧,对于 Java 或任何语言,这些定义始终适用:

  • caller — the function is invoking the function call.

  • callee — the function that is invoked by the caller.

  • caller — 函数正在调用函数调用。

  • callee — 调用者调用的函数。

An argument term technically in programming refers to the data that is passed by caller to the callee.

从技术上讲,编程中的参数术语是指调用方传递给被调用方的数据。

And a parameter term technically refers to the type of data being passed more specifically to an identifier which identifies the type of data. So a parameter more or less refers to the identifier that identifies a particular type. Coming further, a formal parameter is the identifier used in the callee's method signature.

并且参数术语在技术上是指传递给标识数据类型的标识符的数据类型。因此,参数或多或少是指标识特定类型的标识符。更进一步,形式参数是被调用方方法签名中使用的标识符。

And an actual parameter is the identifier used by the caller when invoking the call.

而实际参数是调用者在调用调用时使用的标识符。

Since you know that we can even pass the arguments (i.e. data) in the call to the callee directly, actual parameters are not compulsory and hence directly data can be passed whereas formal parameters are always compulsory.

由于您知道我们甚至可以将调用中的参数(即数据)直接传递给被调用者,因此实参不是强制性的,因此可以直接传递数据,而形式参数始终是强制性的。