תחום וגישה
הוצג
7:25
יצירת מחלקות
הנה סטודנט, מאוחסן כשלושה משתנים נפרדים: שם, שנה ומדורג. זה עובד. כעת הוסף אלפי סטודנטים. תצטרך שלושת אלפים משתנים…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| 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.
התחום של משתנה הוא...
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
התחום הוא המקום בו השם תקף.
לאחר שהזמנת מתודה רגילה הסתיימה, האם המכנה יכול להשתמש במשתנה מקומי שהוכרז רק בתוך המתודה על ידי שמה המקומי?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
השם המקומי אינו בטווח התוקף של המכנה. אובייקט המיוחס על ידי שם זה יכול עם זאת להיות מוחזר ולשאר נגיש.
איזה סוג של משתנה צריך לשמור על מצב שאובייקט חייב לזכור בין קריאות שיטה?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
משתני אינסטנציה נשארים קיימים לאורך חיי האובייקט.
פרמטר בעל אותו שם כמו משתנה אינסטנציה מוסך (מסתיר) את משתנה האינסטנציה בתוך השיטה.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
בתוך המטהודה, השם מתייחס לפראמטר; השתמש ב-this כדי לגשת לשדה.
כאשר פראמטר מחסה משתנה מוחלט באותו שם, נאמר שהוא ___ אותו (מילה אחת).
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
הצללה מסתירה את השדה מאחורי השם המקומי.
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.
טווח תוקף, מצב שדה ורישוי גישה
להבין בין הכרזות לבין הקשר הגישה; אובייקט נוסף אינו חסום באופן אוטומטי.
המתודה הסטטית readCount המוצגת יכולה לקרוא other.count כאשר other אינו null.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
היא משתמשת ביחוס אובייקט מפורש ונמצאת בתוך המחלקה שמכריזה על השדה ה-private; אין צורך ב-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 ללא שינוי בשדה.
מה השתבש? רק צוות האתר רואה זאת.