חלקPart 3: OOP: היסודותOOP: the basics
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
בנאים ו-thisConstructors & this
בנאים, overloading של בנאים, this() ו-this.field, וסדר האתחול.
Constructors, constructor overloading, this() and this.field, and initialization order.
הנקודות החשובותKey points
- בנאי: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-
new:constructor: A special "method" named after the class, with no return type, that runs onnew: - this: הפניה לעצם הנוכחי.this: A reference to the current object.
- בנאי רץ ב-
newומקבל ערכים.A constructor runs onnewand takes values. this.titleהוא השדה,titleהוא הפרמטר.this.titleis the field,titleis the parameter.- בנאי קצר מעביר את העבודה לבנאי המלא עם
this(title, "unknown").A short constructor hands the work to the full one withthis(title, "unknown"). - כך האתחול כתוב במקום אחד.That way the setup lives in one place.
- ברגע שכתבתם
Book(String title), Java כבר לא נותנתBook().As soon as you writeBook(String title), Java no longer gives youBook(). - javac עוצר ומסביר מה נדרש ומה נמצא.javac stops and explains what's required and what was found.
- כשהבנאי מתחיל,
aכבר 5 (האתחול שליד השדה רץ קודם), ו-bעדיין 0.When the constructor starts,ais already 5 (the initializer next to the field ran first), andbis still 0.
מונחיםTerms
- בנאיconstructor: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-
new:Book(String title) { this.title = title; }.A special "method" named after the class, with no return type, that runs onnew:Book(String title) { this.title = title; }. - thisthis: הפניה לעצם הנוכחי.
this.titleהוא השדה, ו-titleלבד הוא הפרמטר אם יש פרמטר בשם הזה.A reference to the current object.this.titleis the field, and plaintitleis the parameter if there's a parameter with that name. - שרשור בנאיםconstructor chaining:
this(...)בשורה הראשונה של בנאי קורא לבנאי אחר של אותה מחלקה. כך כותבים את האתחול פעם אחת, ובנאים קצרים מוסיפים ברירות מחדל.this(...)on a constructor's first line calls another constructor of the same class. That way the setup is written once, and short constructors add defaults. - בנאי ברירת מחדלdefault constructor: אם לא כתבתם שום בנאי, Java נותנת בנאי ריק בלי פרמטרים. ברגע שכתבתם בנאי כלשהו, הוא נעלם.If you wrote no constructor, Java gives an empty one with no parameters. As soon as you write any constructor, it's gone.
- סדר האתחולinitialization order: ב-
new: כל השדות מקבלים 0,nullאוfalse, אחר כך רצים האתחולים שליד השדות (int a = 5;), ורק אז גוף הבנאי.Onnew: every field gets 0,nullorfalse, then the initializers next to the fields run (int a = 5;), and only then the constructor body. - הסתרהshadowing: כשלפרמטר או למשתנה מקומי יש שם של שדה, הוא "מסתיר" את השדה.
title = title;משים את הפרמטר לעצמו, והשדה נשארnull.When a parameter or local variable has a field's name, it "hides" the field.title = title;assigns the parameter to itself, and the field staysnull. - בנאיconstructor: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-
new:Book(String title) { this.title = title; }.A special "method" named after the class, with no return type, that runs onnew:Book(String title) { this.title = title; }. - thisthis: הפניה לעצם הנוכחי.
this.titleהוא השדה, ו-titleלבד הוא הפרמטר אם יש פרמטר בשם הזה.A reference to the current object.this.titleis the field, and plaintitleis the parameter if there's a parameter with that name. - הסתרהshadowing: כשלפרמטר או למשתנה מקומי יש שם של שדה, הוא "מסתיר" את השדה.
title = title;משים את הפרמטר לעצמו, והשדה נשארnull.When a parameter or local variable has a field's name, it "hides" the field.title = title;assigns the parameter to itself, and the field staysnull. - שרשור בנאיםconstructor chaining:
this(...)בשורה הראשונה של בנאי קורא לבנאי אחר של אותה מחלקה. כך כותבים את האתחול פעם אחת, ובנאים קצרים מוסיפים ברירות מחדל.this(...)on a constructor's first line calls another constructor of the same class. That way the setup is written once, and short constructors add defaults. - בנאי ברירת מחדלdefault constructor: אם לא כתבתם שום בנאי, Java נותנת בנאי ריק בלי פרמטרים. ברגע שכתבתם בנאי כלשהו, הוא נעלם.If you wrote no constructor, Java gives an empty one with no parameters. As soon as you write any constructor, it's gone.
- סדר האתחולinitialization order: ב-
new: כל השדות מקבלים 0,nullאוfalse, אחר כך רצים האתחולים שליד השדות (int a = 5;), ורק אז גוף הבנאי.Onnew: every field gets 0,nullorfalse, then the initializers next to the fields run (int a = 5;), and only then the constructor body.
Cheatsheet
בנאי: אתחול בשורה אחתA constructor: setup in one line
Book a = new Book("Dune", 1965);
Book b = new Book("Emma", 1815);
System.out.println(a.title + " " + a.year + ", " + b.title + " " + b.year);כמה בנאים ו-this(...)Several constructors and this(...)
Book a = new Book("Dune", "Herbert");
Book b = new Book("Notes");
System.out.println(a.title + " by " + a.author);
System.out.println(b.title + " by " + b.author);סדר האתחולInitialization order
Demo d = new Demo();
System.out.println("after: a=" + d.a + " b=" + d.b);שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
constructor Book in class Book cannot be applied to given ty | הבנאי הריק נעלםThe empty constructor disappears | constructor Book in class Book cannot be applied to given types;: יש רק בנאי שמקבל String, ו-new Book() לא נותן כלום.constructor Book in class Book cannot be applied to given types;: there's only a constructor taking a String, and new Book() gives nothing. |
constructor Book in class Book cannot be applied to given ty | צייד באגים: אין בנאי ריקBug hunter: no empty constructor | constructor Book in class Book cannot be applied to given types;: הבנאי היחיד דורש String. מוסיפים Book() { this("Untitled"); }.constructor Book in class Book cannot be applied to given types;: the only constructor needs a String. Add Book() { this("Untitled"); }. |