Method Signatures · Firmas de Métodos
| English | Español |
|---|---|
| method/ˈmeθəd/ | método |
| signature/ˈsɪɡnɪtʃə/ | firma |
| parameter/pəˈræmɪtə/ | parámetro |
| argument/ˈɑːɡjuːmənt/ | argumento |
| overloading/ˌəʊvəˈləʊdɪŋ/ | sobrecarga |
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.
La etiqueta del nombre de un método
- Un método 方法 es un bloque de código con nombre que puedes llamar para realizar una tarea.
- Su firma 方法签名 es la parte que lo identifica: el nombre más la lista de parámetros.
- Ejemplo de firma:
max(int a, int b)— nombremax, dos parámetrosint. - La firma es cómo Java sabe cuál método te refieres cuando llamas a uno.
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.
Parámetros y argumentos
- Un parámetro 形参 es una variable en la definición del método:
int aenmax(int a, int b). - Un argumento 实参 es el valor real que pasas al llamarlo:
max(5, 9)pasa5y9. - Los argumentos se asignan a los parámetros por posición, de izquierda a derecha.
- Dentro del método, los parámetros contienen copias de los argumentos.
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.
El tipo de retorno
- El tipo de retorno 返回类型的 de un método indica qué tipo de valor devuelve.
public int max(int a, int b)retorna unint; un métodovoidno retorna nada.- Usa
return value;para enviar un resultado de vuelta al llamador. - El tipo de retorno forma parte del encabezado pero no forma parte de la firma.
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).
Sobrecarga: mismo nombre, diferente firma
- Java permite la sobrecarga 重载: varios métodos con el mismo nombre pero listas de parámetros diferentes.
print(int x)yprint(String s)son distintos; los tipos de argumento los diferencian.- El compilador selecciona la versión cuya firma coincida con tus argumentos.
- La sobrecarga solo funciona cuando las firmas difieren (el tipo de retorno por sí solo no es suficiente).
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.
La firma es nombre + lista de parámetros — el tipo de retorno NO forma parte de ella. Por eso no puedes sobrecargar dos métodos que difieran solo por el tipo de retorno. Parámetros viven en la definición; argumentos son los valores que pasas, asignados por posición. Pasar el número o tipo incorrecto de argumentos genera un error de compilación.
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.
Llamando a max:
- Definición:
public int max(int a, int b) { return a > b ? a : b; } - Llamada:
int m = max(5, 9);→ argumento5→ parámetroa,9→b. - El método retorna
9, almacenado enm.
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.
Una firma de método es su nombre más lista de parámetros (el tipo de retorno no forma parte de ella). Parámetros son las variables de la definición; argumentos son los valores pasados en una llamada, asignados por posición. Sobrecarga permite el mismo nombre con listas de parámetros diferentes, distinguidos por los argumentos.
A method call returns a value · Una llamada a método devuelve un valor
Arguments 5 and 9 bind to parameters a and b; the method returns 9. · Los argumentos 5 y 9 se vinculan a los parámetros a y b; el método devuelve 9.
A method's signature consists of its... · La firma de un método consiste en su...
Signature = name + parameters; the return type is not part of it. · Firma = nombre + parámetros; el tipo de retorno no forma parte de ella.
In max(5, 9), the values 5 and 9 are the... · En max(5, 9), los valores 5 y 9 son los...
Arguments are the values passed; parameters live in the definition. · Los argumentos son los valores pasados; los parámetros viven en la definición.
You can overload two methods that differ only in their return type. · Puede sobrecargar dos métodos que difieran únicamente en su tipo de retorno.
The return type isn't part of the signature, so that's not allowed. · El tipo de retorno no es parte de la firma, por lo que eso no está permitido.
Having several methods with the same name but different parameter lists is called... · Tener varios métodos con el mismo nombre pero diferentes listas de parámetros se llama...
Overloading = same name, different parameter lists. · Sobrecarga = mismo nombre, diferentes listas de parámetros.
Arguments are matched to parameters by their ___ (one word), left to right. · Los argumentos se emparejan con los parámetros por su ___ (una palabra), de izquierda a derecha.
Positional matching: first argument → first parameter. · Emparejamiento posicional: primer argumento → primer parámetro.