Calling Instance Methods · Llamada a Métodos de Instancia
| English | Español |
|---|---|
| instance method/ˈɪnstəns ˈmeθəd/ | método de instancia |
Calling on an object
- An instance method 实例方法 is called on a specific object, using dot notation.
word.length()callslength()on the objectword.- The object before the dot is the one the method acts on.
- Unlike a class method, you need a real object to call it.
Llamada a un objeto
- Un método de instancia se llama sobre un objeto específico, usando notación de puntos.
word.length()llama alength()en el objetoword.- El objeto antes del punto es sobre el cual actúa el método.
- A diferencia de un método de clase, necesitas un objeto real para llamarlo.
The object is the context
- Inside an instance method, the code works with that object's state.
"cat".length()returns3;"hello".length()returns5— same method, different objects.- The method reads (or changes) the state of the object it was called on.
- Two objects of one class can give different results from the same call.
El objeto es el contexto
- Dentro de un método de instancia, el código trabaja con el estado de ese objeto.
"cat".length()devuelve3;"hello".length()devuelve5— mismo método, diferentes objetos.- El método lee (o modifica) el estado del objeto sobre el que fue llamado.
- Dos objetos de una misma clase pueden dar resultados distintos ante la misma llamada.
Arguments and return values
- Instance methods can take arguments and return a value, just like class methods.
"hello".substring(1, 3)passes1and3, returns"el".- Capture the returned value:
String part = "hello".substring(1, 3); - The dot's object plus the arguments together decide the result.
Argumentos y valores de retorno
- Los métodos de instancia pueden recibir argumentos y devolver un valor, al igual que los métodos de clase.
"hello".substring(1, 3)pasa1y3, y devuelve"el".- Captura el valor devuelto:
String part = "hello".substring(1, 3); - El objeto tras el punto más los argumentos determinan juntos el resultado.
Instance vs. class, side by side
- Class method:
Math.sqrt(16)— dot after a class name. - Instance method:
word.length()— dot after an object. - If it needs a specific object's data, it's an instance method.
- If it's a general utility with no object, it's a class (static) method.
Instancia vs. clase, lado a lado
- Método de clase:
Math.sqrt(16)— punto después de un nombre de clase. - Método de instancia:
word.length()— punto después de un objeto. - Si necesita datos específicos de un objeto, es un método de instancia.
- Si es una utilidad general sin objeto asociado, es un método de clase (estático).
An instance method needs an object before the dot: word.length(), not String.length(). The object before the dot supplies the state the method acts on, so the same call gives different results on different objects. Class methods (Math.sqrt) use a class name instead — knowing which is which tells you whether you need an object first.
Un método de instancia necesita un objeto antes del punto: word.length(), no String.length(). El objeto antes del punto proporciona el estado sobre el cual actúa el método, por lo que la misma llamada da resultados diferentes en diferentes objetos. Los métodos de clase (Math.sqrt) usan un nombre de clase en su lugar — saber cuál es cuál te indica si necesitas un objeto primero.
Same method, different objects:
String a = "cat", b = "tiger";a.length()→3(acts on"cat").b.length()→5(acts on"tiger") — the object before the dot decides.
Mismo método, diferentes objetos:
String a = "cat", b = "tiger";a.length()→3(actúa sobre"cat").b.length()→5(actúa sobre"tiger") — el objeto antes del punto decide.
An instance method is called on an object with dot notation (word.length()); the object before the dot supplies the state the method works with. It can take arguments and return a value like any method. This contrasts with a class (static) method, called on a class name (Math.sqrt(16)).
Un método de instancia se llama sobre un objeto con notación de puntos (word.length()); el objeto antes del punto proporciona el estado con el que trabaja el método. Puede recibir argumentos y devolver un valor como cualquier método. Esto contrasta con un método de clase (estático), que se llama sobre un nombre de clase (Math.sqrt(16)).
An instance method acts on its object · Un método de instancia actúa sobre su objeto
length() reads the state of the object before the dot. · length() lee el estado del objeto antes del punto.
Which is a correct call to the instance method length on a String word? · ¿Cuál es una llamada correcta al método de instancia length en un String word?
Instance methods are called on an object: object.method(). · Los métodos de instancia se llaman sobre un objeto: object.method().
What does "tiger".length() return? · ¿Qué devuelve "tiger".length()?
"tiger" has 5 characters. · "tiger" tiene 5 caracteres.
Math.sqrt(16) is a class method and word.length() is an instance method. The difference is... · Math.sqrt(16) es un método de clase y word.length() es un método de instancia. La diferencia es...
Class name before the dot → class method; object → instance method. · Nombre de clase antes del punto → método de clase; objeto → método de instancia.
The same instance-method call can give different results on different objects. · La misma llamada a un método de instancia puede dar resultados diferentes en diferentes objetos.
The object before the dot supplies the state. · El objeto antes del punto proporciona el estado.
An instance method is called on a specific ___ using dot notation (one word). · Un método de instancia se llama sobre un ___ específico usando notación de puntos (una palabra).
object.method() — the object supplies the context. · object.method() — el objeto proporciona el contexto.