Chữ ký Phương thức
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| method/ˈmeθəd/ | Phương thức |
| signature/ˈsɪɡnɪtʃə/ | Ký hiệu phương thức |
| parameter/pəˈræmɪtə/ | Tham số hình thức |
| argument/ˈɑːɡjuːmənt/ | Tham số thực |
| overloading/ˌəʊvəˈləʊdɪŋ/ | Tải lại (overloading) |
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.
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.

The parts of a method signature and a call that matches it
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.
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).
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.
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.
Một lời gọi phương thức trả về một giá trị
Tham số 5 và 9 gắn vào tham số a và b; phương thức trả về 9.
Chữ ký của một phương thức bao gồm...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Chữ ký = tên + tham số; kiểu trả về không phải là một phần của nó.
Trong max(5, 9), các giá trị 5 và 9 là...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Tham số (Arguments) là các giá trị được truyền; tham số (parameters) sống trong định nghĩa.
Bạn có thể ghi đè hai phương thức chỉ khác nhau ở kiểu trả về.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Kiểu trả về không phải là một phần của chữ ký, nên điều đó không được phép.
Có nhiều phương thức cùng tên nhưng danh sách tham số khác nhau được gọi là...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Ghi đè = cùng tên, danh sách tham số khác nhau.
Tham số được khớp với tham số dựa trên ___ (một từ), từ trái sang phải.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Khớp vị trí: tham số đầu tiên → tham số đầu tiên.