Calling Instance Methods · Chamando Métodos de Instância
| English | Português |
|---|---|
| instance method/ˈɪnstəns ˈmeθəd/ | método de instância |
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.
Chamando em um objeto
- Um método de instância 实例方法 é chamado em um objeto específico, usando notação de ponto.
word.length()chamalength()no objetoword.- O objeto antes do ponto é aquele sobre o qual o método atua.
- Diferente de um método de classe, você precisa de um objeto real para chamá-lo.
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.
O objeto é o contexto
- Dentro de um método de instância, o código opera com o estado aquele objeto.
"cat".length()retorna3;"hello".length()retorna5— mesmo método, objetos diferentes.- O método lê (ou altera) o estado do objeto sobre o qual foi chamado.
- Dois objetos de uma mesma classe podem dar resultados diferentes da mesma chamada.
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 e valores de retorno
- Métodos de instância podem receber argumentos e retornar um valor, assim como métodos de classe.
"hello".substring(1, 3)passa1e3, retorna"el".- Capture o valor retornado:
String part = "hello".substring(1, 3); - O objeto do ponto mais os argumentos juntos decidem o 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.
Instância vs. classe, lado a lado
- Método de classe:
Math.sqrt(16)— ponto após um nome de classe. - Método de instância:
word.length()— ponto após um objeto. - Se precisar dos dados específicos de um objeto, é um método de instância.
- Se for uma utilidade geral sem objeto, é um método de classe (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.
Um método de instância precisa de um objeto antes do ponto: word.length(), não String.length(). O objeto antes do ponto fornece o estado que o método utiliza, então a mesma chamada dá resultados diferentes em objetos diferentes. Métodos de classe (Math.sqrt) usam um nome de classe — saber qual é qual diz se você precisa de um objeto primeiro.
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.
Mesmo método, objetos diferentes:
String a = "cat", b = "tiger";a.length()→3(age em"cat").b.length()→5(age em"tiger") — o objeto antes do ponto 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)).
Um método de instância é chamado em um objeto com notação de ponto (word.length()); o objeto antes do ponto fornece o estado que o método usa. Ele pode receber argumentos e retornar um valor como qualquer método. Isso contrasta com um método de classe (estático), chamado em um nome de classe (Math.sqrt(16)).
An instance method acts on its object · Um método de instância atua sobre seu objeto
length() reads the state of the object before the dot. · length() lê o estado do objeto antes do ponto.
Which is a correct call to the instance method length on a String word? · Qual é uma chamada correta ao método de instância length em uma String word?
Instance methods are called on an object: object.method(). · Métodos de instância são chamados em um objeto: object.method().
What does "tiger".length() return? · O que "tiger".length() retorna?
"tiger" has 5 characters. · "tiger" tem 5 caracteres.
Math.sqrt(16) is a class method and word.length() is an instance method. The difference is... · Math.sqrt(16) é um método de classe e word.length() é um método de instância. A diferença é...
Class name before the dot → class method; object → instance method. · Nome de classe antes do ponto → método de classe; objeto → método de instância.
The same instance-method call can give different results on different objects. · A mesma chamada de método de instância pode dar resultados diferentes em objetos diferentes.
The object before the dot supplies the state. · O objeto antes do ponto fornece o estado.
An instance method is called on a specific ___ using dot notation (one word). · Um método de instância é chamado em um ___ específico usando notação de ponto (uma palavra).
object.method() — the object supplies the context. · object.method() — o objeto fornece o contexto.