Gọi Phương thức Instance
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| instance method/ˈɪnstəns ˈmeθəd/ | Phương thức thực thể |
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.
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.

One class, many objects: each instance has its own field values
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.
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.
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.
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.
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)).
Một phương thức instance tác động lên đối tượng của nó
length() đọc trạng thái của đối tượng trước dấu chấm.
Biểu thức nào là cách gọi đúng phương thức instance length trên String word?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Phương thức instance được gọi trên một đối tượng: object.method().
"tiger".length() trả về giá trị gì?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
"tiger" có 5 ký tự.
Math.sqrt(16) là phương thức lớp và word.length() là phương thức instance. Sự khác biệt là...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Tên lớp trước dấu chấm → phương thức lớp; đối tượng → phương thức instance.
Cùng một lời gọi phương thức instance có thể trả về kết quả khác nhau trên các đối tượng khác nhau.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Đối tượng trước dấu chấm cung cấp trạng thái.
Một phương thức instance được gọi trên một ___ cụ thể sử dụng ký hiệu chấm (một từ).
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
object.method() — đối tượng cung cấp ngữ cảnh.