Method Signatures · 方法签名
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| method/ˈmeθəd/ | 方法 | fāng fǎ |
| signature/ˈsɪɡnɪtʃə/ | 方法签名 | fāng fǎ qiān míng |
| parameter/pəˈræmɪtə/ | 形参 | xíng cān |
| argument/ˈɑːɡjuːmənt/ | 实参 | shí cān |
| overloading/ˌəʊvəˈləʊdɪŋ/ | 重载 | zhòng zài |
A method's name tag
- A method 方法 is a named block of code you can call to do a job.
- Its signature 方法签名 is the part that identifies it: the name plus the parameter list.
- Example signature:
max(int a, int b)— namemax, twointparameters. - The signature is how Java knows which method you mean when you call one.
方法的名牌
- 方法是一段有名字的代码块,你可以调用它来做一件事。
- 它的方法签名是标识它的那部分:名字加上参数列表。
- 签名示例:
max(int a, int b)——名字max,两个int参数。 - 当你调用一个方法时,Java 靠签名知道你指的是哪一个。
Parameters and arguments
- A parameter 形参 is a variable in the method's definition:
int ainmax(int a, int b). - An argument 实参 is the actual value you pass when calling:
max(5, 9)passes5and9. - Arguments are matched to parameters by position, left to right.
- Inside the method, the parameters hold copies of the arguments.
形参与实参
- 形参是方法定义里的一个变量:
max(int a, int b)中的int a。 - 实参是你调用时实际传入的值:
max(5, 9)传入5和9。 - 实参按位置、从左到右匹配到形参。
- 在方法内部,形参保存着实参的副本。
The return type
- A method's return type says what kind of value it hands back.
public int max(int a, int b)returns anint; avoidmethod returns nothing.- Use
return value;to send a result back to the caller. - The return type is part of the header but not part of the signature.
返回类型
- 方法的返回类型说明它交还什么样的值。
public int max(int a, int b)返回一个int;void方法什么也不返回。- 用
return value;把结果送回给调用者。 - 返回类型是方法头的一部分,但不是签名的一部分。
Overloading: same name, different signature
- Java allows overloading 重载: several methods with the same name but different parameter lists.
print(int x)andprint(String s)are distinct — the argument types tell them apart.- The compiler picks the version whose signature matches your arguments.
- Overloading only works when the signatures differ (return type alone is not enough).
重载:同名,不同签名
- Java 允许重载:几个同名但参数列表不同的方法。
print(int x)和print(String s)是不同的——实参类型把它们区分开。- 编译器挑选签名与你实参匹配的那个版本。
- 重载只在签名不同时才有效(仅返回类型不同不够)。
The signature is name + parameter list — the return type is NOT part of it. That's why you can't overload two methods that differ only by return type. Parameters live in the definition; arguments are the values you pass, matched by position. Passing the wrong number or type of arguments is a compile error.
**签名是名字 + 参数列表——返回类型不属于它。**这就是为什么你不能重载两个仅返回类型不同的方法。形参存在于定义里;实参是你传入的值,按位置匹配。传入错误数量或类型的实参是一个编译错误。
Calling max:
- Definition:
public int max(int a, int b) { return a > b ? a : b; } - Call:
int m = max(5, 9);→ argument5→ parametera,9→b. - The method returns
9, stored inm.
调用 max:
- 定义:
public int max(int a, int b) { return a > b ? a : b; } - 调用:
int m = max(5, 9);→ 实参5→ 形参a,9→b。 - 方法返回
9,存进m。
A method signature is its name plus parameter list (the return type is not part of it). Parameters are the definition's variables; arguments are the values passed at a call, matched by position. Overloading allows the same name with different parameter lists, distinguished by the arguments.
方法签名是它的名字加参数列表(返回类型不属于它)。形参是定义的变量;实参是调用时传入的值,按位置匹配。重载允许同名但参数列表不同,靠实参区分。
A method call returns a value · 一次方法调用返回一个值
Arguments 5 and 9 bind to parameters a and b; the method returns 9. · 实参 5 和 9 绑定到形参 a 和 b;方法返回 9。
A method's signature consists of its... · 一个方法的签名由它的……组成。
Signature = name + parameters; the return type is not part of it. · 签名 = 名字 + 参数;返回类型不属于它。
In max(5, 9), the values 5 and 9 are the... · 在 max(5, 9) 中,值 5 和 9 是……
Arguments are the values passed; parameters live in the definition. · 实参是传入的值;形参在定义里。
You can overload two methods that differ only in their return type. · 你可以重载两个仅返回类型不同的方法。
The return type isn't part of the signature, so that's not allowed. · 返回类型不属于签名,所以不允许。
Having several methods with the same name but different parameter lists is called... · 几个同名但参数列表不同的方法叫做……
Overloading = same name, different parameter lists. · 重载 = 同名,不同参数列表。
Arguments are matched to parameters by their ___ (one word), left to right. · 实参按它们的 ___(填英文一词 position)从左到右匹配到形参。
Positional matching: first argument → first parameter. · 按位置匹配:第一个实参 → 第一个形参。