Calling Instance Methods · Appel de Méthodes d'Instance
| English | Français |
|---|---|
| instance method/ˈɪnstəns ˈmeθəd/ | méthode d'instance |
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.
Appeler sur un objet
- Une méthode d'instance 实例方法 est appelée sur un objet spécifique, en utilisant la notation pointée.
word.length()appellelength()sur l'objetword.- L'objet avant le point est celui sur lequel la méthode agit.
- Contrairement à une méthode de classe, vous avez besoin d'un objet réel pour l'appeler.
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.
L'objet est le contexte
- Dans une méthode d'instance, le code travaille avec l'état de cet objet.
"cat".length()retourne3;"hello".length()retourne5— même méthode, objets différents.- La méthode lit (ou modifie) l'état de l'objet sur lequel elle a été appelée.
- Deux objets d'une même classe peuvent donner des résultats différents pour le même appel.
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.
Paramètres et valeurs de retour
- Les méthodes d'instance peuvent prendre des paramètres et retourner une valeur, tout comme les méthodes de classe.
"hello".substring(1, 3)passe1et3, retourne"el".- Capturer la valeur retournée :
String part = "hello".substring(1, 3); - L'objet du point combiné aux paramètres décident du résultat.
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.
Instance vs. classe, côte à côte
- Méthode de classe :
Math.sqrt(16)— point après un nom de classe. - Méthode d'instance :
word.length()— point après un objet. - Si elle a besoin des données spécifiques d'un objet, c'est une méthode d'instance.
- Si c'est une utilité générale sans objet, c'est une méthode de classe (statique).
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.
Une méthode d'instance a besoin d'un objet avant le point : word.length(), pas String.length(). L'objet avant le point fournit l'état sur lequel la méthode agit, donc le même appel donne des résultats différents sur des objets différents. Les méthodes de classe (Math.sqrt) utilisent un nom de classe à la place — savoir laquelle est laquelle vous dit si vous avez besoin d'un objet en premier.
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.
Même méthode, objets différents :
String a = "cat", b = "tiger";a.length()→3(agit sur"cat").b.length()→5(agit sur"tiger") — l'objet avant le point décide.
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)).
Une méthode d'instance est appelée sur un objet avec la notation pointée (word.length()) ; l'objet avant le point fournit l'état avec lequel la méthode travaille. Elle peut prendre des paramètres et retourner une valeur comme n'importe quelle méthode. Cela contraste avec une méthode de classe (statique), appelée sur un nom de classe (Math.sqrt(16)).
An instance method acts on its object · Une méthode d'instance agit sur son objet
length() reads the state of the object before the dot. · length() lit l'état de l'objet avant le point.
Which is a correct call to the instance method length on a String word? · Quelle est un appel correct à la méthode d'instance length sur une chaîne word ?
Instance methods are called on an object: object.method(). · Les méthodes d'instance sont appelées sur un objet : object.method().
What does "tiger".length() return? · Que retourne "tiger".length() ?
"tiger" has 5 characters. · "tiger" a 5 caractères.
Math.sqrt(16) is a class method and word.length() is an instance method. The difference is... · Math.sqrt(16) est une méthode de classe et word.length() est une méthode d'instance. La différence est...
Class name before the dot → class method; object → instance method. · Nom de classe avant le point → méthode de classe ; objet → méthode d'instance.
The same instance-method call can give different results on different objects. · Le même appel de méthode d'instance peut donner des résultats différents selon les objets.
The object before the dot supplies the state. · L'objet avant le point fournit l'état.
An instance method is called on a specific ___ using dot notation (one word). · Une méthode d'instance est appelée sur un ___ spécifique en utilisant la notation ponctuelle (un seul mot).
object.method() — the object supplies the context. · object.method() — l'objet fournit le contexte.