Calling Instance Methods · 调用实例方法
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| instance method/ˈɪnstəns ˈmeθəd/ | 实例方法 | shí lì fāng fǎ |
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.
在对象上调用
- 实例方法用点记法在一个具体对象上调用。
word.length()在对象word上调用length()。- 点之前的对象就是方法作用的那一个。
- 不同于类方法,你需要一个真实对象才能调用它。
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.
对象就是上下文
- 在实例方法内部,代码处理的是那个对象的状态。
"cat".length()返回3;"hello".length()返回5——同一个方法,不同对象。- 方法读取(或改变)它被调用时所在对象的状态。
- 同一个类的两个对象,对同一个调用可以给出不同结果。
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.
实参与返回值
- 实例方法可以接受实参并返回一个值,就像类方法一样。
"hello".substring(1, 3)传入1和3,返回"el"。- 接住返回的值:
String part = "hello".substring(1, 3); - 点的对象加上实参一起决定结果。
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.
实例对类,并排看
- 类方法:
Math.sqrt(16)——点在类名之后。 - 实例方法:
word.length()——点在对象之后。 - 如果它需要某个具体对象的数据,就是实例方法。
- 如果它是一个不需要对象的通用工具,就是类(静态)方法。
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.
**实例方法在点之前需要一个对象:word.length(),而非 String.length()。**点之前的对象提供方法作用的状态,所以同一个调用在不同对象上给出不同结果。类方法(Math.sqrt)改用类名——分清哪个是哪个,就知道你是否需要先有一个对象。
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.
同一个方法,不同对象:
String a = "cat", b = "tiger";a.length()→3(作用于"cat")。b.length()→5(作用于"tiger")——点之前的对象决定。
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)).
实例方法用点记法在一个对象上调用(word.length());点之前的对象提供方法处理的状态。它像任何方法一样可以接受实参并返回一个值。这与在类名上调用的类(静态)方法(Math.sqrt(16))形成对比。
An instance method acts on its object · 实例方法作用于它的对象
length() reads the state of the object before the dot. · length() 读取点之前对象的状态。
Which is a correct call to the instance method length on a String word? · 对一个 String word,哪个是对实例方法 length 的正确调用?
Instance methods are called on an object: object.method(). · 实例方法在对象上调用:object.method()。
What does "tiger".length() return? · "tiger".length() 返回什么?
"tiger" has 5 characters. · "tiger" 有 5 个字符。
Math.sqrt(16) is a class method and word.length() is an instance method. The difference is... · Math.sqrt(16) 是类方法,word.length() 是实例方法。区别是……
Class name before the dot → class method; object → instance method. · 点前是类名 → 类方法;对象 → 实例方法。
The same instance-method call can give different results on different objects. · 同一个实例方法调用在不同对象上可以给出不同结果。
The object before the dot supplies the state. · 点之前的对象提供状态。
An instance method is called on a specific ___ using dot notation (one word). · 实例方法用点记法在一个具体的 ___ 上调用(填英文一词 object)。
object.method() — the object supplies the context. · object.method()——对象提供上下文。