The this Keyword
แนะนำใหม่
7:20
การสร้างคลาส
นี่คือนักเรียน ซึ่งจัดเก็บเป็นตัวแปรอิสระสามตัว: ชื่อ, ปีการศึกษา, และเกรด. นั่นทำงานได้ดี.ตอนนี้เพิ่มนักเรียนอีกพันคน. คุณจำเป็นต้องใช้ตัวแปรสามพันตัว…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
A parameter can hide the field it should update
- An object has name "Ada". setName(String name) runs name = name; with the argument "Bo".
- Both unqualified names refer to the parameter, so the object still has "Ada". this.name = name assigns the parameter value to the field instead.

"This object"
- Inside an instance method,
thisrefers to the object the method was called on.student.addScore(10)runs withthis= thatstudent. this.scoremeans "thescoreof the object I'm working on." It lets a method talk about its own object explicitly.
Distinguishing field from parameter
- The main use: tell an instance variable apart from a parameter of the same name.
void setName(String name) { this.name = name; } this.nameis the instance variable; plainnameis the parameter. This is the standard fix for the shadowing bug from 3.8.
Access to the receiver object
thisgives a method a handle on the exact object it was called on. Every instance method has an implicitthis, whether you write it or not.- When score names an unshadowed instance field,
score += s;usesthis.score. If a parameter or local named score shadows it, the unqualified name instead refers to that variable. You writethisexplicitly only when needed (like resolving shadowing).
this ชี้ถึงวัตถุผู้รับ (receiver object)
ภายใน setName, this คือวัตถุ ada ที่ถูกเรียกใช้
Inside an instance method, this refers to...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
this คือตัวชี้ไปยังวัตถุปัจจุบัน
ใน setName(String name) { this.name = name; }, this.name คือ...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
this.name คือฟิลด์; ส่วน name ล้วนๆ คือพารามิเตอร์
คีย์เวิร์ด this สามารถใช้ในวิธี static ได้
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
วิธี static ไม่มีวัตถุเฉพาะเจาะจง ดังนั้นจึงไม่มี this
การเขียน name = name; (ไม่มี this) ภายใน setName(String name) จะเกิดอะไรขึ้น?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ทั้งสองชื่อคือพารามิเตอร์; ใช้ this.name เพื่อเข้าถึงฟิลด์
ทุกวิธีอินสต্যানซ์มี this โดยนัย ไม่ว่าคุณจะเขียนหรือไม่ได้เขียนก็ตาม
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
วิธีอินสต্যানซ์มีผู้รับปัจจุบัน ถ้า score ไม่มีการระบุชื่อ จะหมายถึง this.score ก็ต่อเมื่อไม่มีพารามิเตอร์หรือตัวแปรในท้องถิ่นที่บดบังฟิลด์นี้
No this in a static method
- A static method has no
this— it isn't tied to any particular object. So a static method cannot usethisor an unqualified instance field, but may read an instance field through an explicit reference when access permission allows it. - Trying to use
thisin a static method is a compile error. If you needthis, the method must be an instance method, not static.
Use this.field = param; to defeat shadowing — this.name is the instance variable, plain name is the parameter. Writing name = name; (no this) just assigns the parameter to itself. And this doesn't exist in a static method — a static method has no particular object, so using this there won't compile.
Type the field value after the faulty setName("Bo") call on the described object.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The parameter assigns to itself; the field is unchanged until this.name is used on the left.
หาก alias และ a ชี้ไปยัง Student คนเดียวกัน การเรียก alias.setName("Di") จะเปลี่ยนชื่อที่จะอ่านผ่าน a ในภายหลัง
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ทั้งสองการอ้างอิงระบุถึงวัตถุผู้รับเดียวกัน whose field ถูกอัปเดต
โค้ดในวัตถุ b เรียก a.setName("Cy"). ภายใน setName วัตถุใดคือ this?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ผู้รับ a ให้ค่า this ไม่ว่าโค้ดจากวัตถุอื่นใดจะเริ่มต้นการเรียกใช้ก็ตาม
Fixing the shadowing bug with this:
- Instance variable
private String name; void setName(String name) { this.name = name; }— correct!this.name(the field) gets the value ofname(the parameter).
Carry the reasoning to a new case
- Trace a and b as separately created students named Ada and Bo.
a.setName("Cy")changes a to Cy while b stays Bo. If alias=a,alias.setName("Di")changes that same first object, so a now reads Di. - The receiver is the object the instance method acts on, not necessarily the object whose code called it. A static method can operate on an explicit object reference, but has no implicit this.
this refers to the current object — the one an instance method was called on. Its key use is resolving shadowing: this.name = name; assigns the parameter to the instance variable. Every instance method has an implicit this, but a static method has none, so this can't be used there.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้