בונים
הוצג
7:25
יצירת מחלקות
הנה סטודנט, מאוחסן כשלושה משתנים נפרדים: שם, שנה ומדורג. זה עובד. כעת הוסף אלפי סטודנטים. תצטרך שלושת אלפים משתנים…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| default constructor/dɪˈfɒlt kənˈstrʌktə/ | בונה ברירת מחדל |
A parameterised constructor does not supply a no-arg one
- Student declares only Student(String n). Creating new Student("Ada") works, but new Student() has no matching constructor.
- Java supplies an implicit default constructor only when none is declared. To allow both calls, explicitly write Student() and Student(String n).

Setting up a new object
- A constructor runs when an object is created with
new, to set its initial state. It has the same name as the class and no return type (not evenvoid). public Student(String n) { name = n; score = 0; }Its job: give the brand-new object sensible starting values.
Constructor parameters
- Parameters let each new object start with its own values.
new Student("Ada")passes"Ada"to the constructor'snparameter. - Inside,
name = n;copies the argument into the instance variable. Each successfulnewexpression creates a distinct object, even when its arguments are identical. Different arguments need not produce different field values if the constructor ignores or normalizes them.
שני ביצועים של new Student("Ada") מתייחסים לאותו אובייקט כי הארגומנטים שווים.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
כל הבערת new מוצלחת בונה אובייקט נפרד; ארגומנטים שווים אינם הופכים את ההפניות לשוות.
Overloading constructors
- A class can have several constructors with different parameter lists — overloading.
Student(String n)andStudent(String n, int s)are both valid. - Java picks the one whose parameters match your
newarguments. This offers callers flexible ways to build an object.
בונים קובעים את מצב ההתחלה
כל אובייקט חדש מוגדר על ידי פרמטרי הבונה.
לבונה יש...
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
אותו שם כמו הקלאס, ללא סוג החזרה — גם לא void.
מתי מתבצע בונה?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
new קורא לבונה כדי לבנות את האובייקט.
אם לא כתבת בונה כלל, Java מספקת בונה ברירת מחדל ללא פרמטרים.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
אך כתיבת כל בונה מבטלת את ברירת המחדל הזו.
ברגע שהגדרת בונה עם פרמטרים, new Student() עדיין עובד אוטומטית.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
הברירת המחדל החינמית נעלמה; כתוב בונה ללא פרמטרים אם תצטרך אותו.
כתיבת שני בונים עם רשימות פרמטרים שונות היא דוגמה ל...
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
אותו שם, פרמטרים שונים = העמסה חוזרת.
The default constructor
- If you write no constructor, Java provides a hidden default constructor 默认构造函数 with no parameters. Fields first have default values (
0,false,null); explicit field initialisers can then set other initial values. - But once you write any constructor, that free default disappears. So if you need a no-arg constructor and a parameterized one, write both.
A constructor has the class's name and NO return type — writing void makes it an ordinary method, not a constructor. And once you define any constructor, Java stops giving you the free no-argument default; if you still want new Student(), you must write that constructor yourself.
Order the steps used to evaluate new Student("Ada").
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
The supplied argument selects a matching constructor; declaring it does not also create a no-argument constructor.
ל-Counter יש value פרטי מסוג int ששווה ל-7; ולקונסטרוקטור אקספליסיטי שמטיל עליו value = 9;. מה הערך של value מיד לאחר הבנייה?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
מאתח השדה מקצה 7 לפני שבוגר הבונה מקצה 9.
Two constructors for Student:
public Student(String n) { name = n; score = 0; }public Student(String n, int s) { name = n; score = s; }new Student("Ada")→ score0;new Student("Bo", 75)→ score75.
Carry the reasoning to a new case
- Compare
class Counter { private int value = 7; }with an otherwise identical class whose explicit constructor assignsvalue = 9;. The first starts at 7 after its field initializer; the second ends construction at 9. - Fields receive default values before field initializers and the constructor body run. A programmer-written no-argument constructor is not the compiler-supplied default constructor, even though both take no arguments.
A constructor (same name as the class, no return type) runs on new to set an object's initial state, using parameters for per-object values. A class can overload several constructors. Write no constructor and Java supplies a default no-arg one — but defining any constructor removes that free default.
מה השתבש? רק צוות האתר רואה זאת.