Scope and Access
แนะนำใหม่
7:20
การสร้างคลาส
นี่คือนักเรียน ซึ่งจัดเก็บเป็นตัวแปรอิสระสามตัว: ชื่อ, ปีการศึกษา, และเกรด. นั่นทำงานได้ดี.ตอนนี้เพิ่มนักเรียนอีกพันคน. คุณจำเป็นต้องใช้ตัวแปรสามพันตัว…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| local variable/ˈləʊkl ˈveərɪəbl/ | ตัวแปรท้องถิ่น |
| scope/skəʊp/ | สโคป |
| shadows/ˈʃædəʊz/ | การบดบังตัวแปร |
A local resets; a field remembers
- A method creates
int count = 0;, increments it and returns 1. Calling that method twice returns 1 both times because each invocation has a fresh local variable. - A method that increments an object's field instead returns 1 and then 2 for the same initially zero-valued object. Distinguish where a name is usable from how long the stored state is retained.

Scope concerns a name in source code
- The scope 作用域 of a declaration is the part of the program where its name can be used. A block local is in scope from its declaration through the remaining block; a for-loop variable is not available after that loop.
- Scope, access permission and lifetime answer different questions. A name may be out of scope even though an object it once referenced remains reachable through another reference; fields are not governed by the same declaration rule as block locals.
Initialize local variables before reading them
- A local variable 局部变量 is declared in a method or block. Java requires it to be definitely assigned before its value is read;
int x; return x;is a compile-time error, not an automatic return of zero. - Instance fields receive default values during construction before initializers and constructor code run. In the example, count starts at 0; that field belongs to each object and persists between its method calls.
public class ScopeCounter {
private int count;
public int localCount() {
int count = 0;
count++;
return count;
}
public int rememberedCount() {
count++;
return count;
}
public void setCount(int count) {
this.count = count;
}
public static int readCount(ScopeCounter other) {
return other.count;
}
}
การประกาศตัวแปรในท้องถิ่น int x; จะกำหนดค่าเริ่มต้นเป็นศูนย์ให้ x โดยอัตโนมัติ ดังนั้น return x; จึงสามารถคอมไพล์ได้
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ตัวแปรในท้องถิ่นต้องได้รับค่าอย่างชัดเจนก่อนนำไปใช้; ค่าเริ่มต้นของฟิลด์ไม่ครอบคลุมตัวแปรในท้องถิ่น
A parameter can hide a field name
- The parameter
countinsetCount(int count)shadows 遮蔽 the field for unqualified uses of that name.count = count;would assign the parameter to itself;this.count = count;updates the receiver's field. - A local may shadow a field too, as localCount demonstrates. Do not infer that every duplicate local declaration is allowed: redeclaring an already in-scope local in a nested block of the same method is a compile-time error.
The scope of a variable is...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Scope is where the name is valid.
หลังจากการเรียกใช้วิธีธรรมดาเสร็จสิ้น ผู้เรียกใช้สามารถใช้ตัวแปรท้องถิ่นที่ประกาศไว้เฉพาะภายในวิธีนั้นด้วยชื่อท้องถิ่นได้หรือไม่?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ชื่อตัวแปรในท้องถิ่นไม่อยู่ในสโคปของผู้เรียกใช้ แต่วัตถุที่อ้างอิงโดยตัวแปรนั้นสามารถส่งกลับและยังคงเข้าถึงได้จากภายนอก
ประเภทของตัวแปรใดควรเก็บสถานะที่วัตถุต้องจดจำระหว่างครั้งของการเรียกใช้วิธี?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Instance variables persist for the object's lifetime.
พารามิเตอร์ที่มีชื่อเดียวกับตัวแปรอินสต্যানซ์จะบดบัง (ซ่อน) ตัวแปรอินสต্যানซ์นั้นภายในวิธี
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ภายในวิธี ชื่อนี้หมายถึงพารามิเตอร์ ให้ใช้ this เพื่อเข้าถึงฟิลด์
เมื่อพารามิเตอร์บังinstance variable ที่มีชื่อเดียวกัน จะเรียกว่า ___ มัน (หนึ่งคำ)
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Shadowing hides the field behind the local name.
Access depends on the declaring class
- Code inside ScopeCounter may read another ScopeCounter object's private count via an explicit reference. The shown static readCount method has no
this, butother.countis valid for a non-null parameter. - An unrelated external class cannot directly read that private field. Private access is not restricted to “only my own object”; an instance method has an implicit receiver, while a static method needs an explicit reference to access instance state.
Do not combine name scope, default initialization and private access into one rule. A local does not automatically start at zero, an instance field cannot be read unqualified from a static method, and a private field of another instance may be accessed inside its declaring class.
ขอบเขต (Scope), สถานะฟิลด์ และสิทธิ์การเข้าถึง
แยกแยะคำประกาศออกจากบริบทการเข้าถึง; Instance อื่นๆ ไม่ได้ถูกปิดกั้นจากการเข้าถึงโดยอัตโนมัติ
เมธอด readCount แบบสแตติกที่แสดงข้างต้นอาจอ่านค่า other.count เมื่อ other มีค่าไม่ใช่ null
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ใช้การอ้างอิงวัตถุแบบชัดเจน และอยู่ในคลาสที่ประกาศฟิลด์ส่วนตัว ไม่จำเป็นต้องใช้ this แบบอ้อม
Predict a new receiver and a returned object
- After two rememberedCount calls on a, its field is 2; a newly constructed b still has count 0. Calling a.localCount() returns 1 without resetting a's field, because its local count shadows that field.
- A method can construct an object in a local variable and return its reference. The local name is then unavailable to the caller, but the returned object can remain reachable; the end of a name's scope does not destroy that object.
Scope tells where a name is usable; access modifiers control permission; object state can outlive a local reference. Initialize locals before reading them, use fields for per-object remembered state, and distinguish shadowing from access to another object's private field.
สำหรับ ScopeCounter a ที่สร้างใหม่ ให้เรียกใช้ a.rememberedCount() สองครั้ง แล้วตามด้วย a.localCount() a.localCount() จะคืนค่าอะไร?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
localCount มีตัวนับท้องถิ่นใหม่เริ่มต้นเป็นศูนย์; มันเพิ่มค่าขึ้นและส่งกลับ 1 โดยไม่เปลี่ยนฟิลด์
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้