נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
בנאים ו-thisConstructors & this
מפגש 14: בנאים, overloading של בנאים, this() ו-this.field, וסדר האתחול.Session 14: Constructors, constructor overloading, this() and this.field, and initialization order.
בנאים, overloading של בנאים, this() ו-this.field, וסדר האתחול. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Constructors, constructor overloading, this() and this.field, and initialization order. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- בנאי: אתחול בשורה אחתA constructor: setup in one line
- כמה בנאים ו-this(...)Several constructors and this(...)
- הבנאי הריק נעלםThe empty constructor disappears
- סדר האתחולInitialization order
- טעויות נפוצותCommon mistakes
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגשThis session's terms
- בנאי: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-
new:constructor: A special "method" named after the class, with no return type, that runs on new: - this: הפניה לעצם הנוכחי.this: A reference to the current object.
- הסתרה: כשלפרמטר או למשתנה מקומי יש שם של שדה, הוא "מסתיר" את השדה.shadowing: When a parameter or local variable has a field's name, it "hides" the field.
- שרשור בנאים:
this(...) בשורה הראשונה של בנאי קורא לבנאי אחר של אותה מחלקה.constructor chaining: this(...) on a constructor's first line calls another constructor of the same class. - בנאי ברירת מחדל: אם לא כתבתם שום בנאי, Java נותנת בנאי ריק בלי פרמטרים.default constructor: If you wrote no constructor, Java gives an empty one with no parameters.
- סדר האתחול: ב-
new:initialization order: On new:
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושאTopic 2
בנאי: אתחול בשורה אחתA constructor: setup in one line
בנאי: אתחול בשורה אחתA constructor: setup in one line
- בנאי רץ ב-
new ומקבל ערכים.A constructor runs on new and takes values. this.title הוא השדה, title הוא הפרמטר.this.title is the field, title is the parameter.- בזיכרון רואים את המסגרת
new Book עם this שמצביע על העצם החדש.In memory you see the new Book frame with this pointing at the new object.
בנאי רץ ב-new ומקבל ערכים. this.title הוא השדה, title הוא הפרמטר. בזיכרון רואים את המסגרת new Book עם this שמצביע על העצם החדש.A constructor runs on new and takes values. this.title is the field, title is the parameter. In memory you see the new Book frame with this pointing at the new object.נושאTopic 3
כמה בנאים ו-this(...)Several constructors and this(...)
כמה בנאים ו-this(...)Several constructors and this(...)
- בנאי קצר מעביר את העבודה לבנאי המלא עם
this(title, "unknown").A short constructor hands the work to the full one with this(title, "unknown"). - כך האתחול כתוב במקום אחד.That way the setup lives in one place.
בנאי קצר מעביר את העבודה לבנאי המלא עם this(title, "unknown"). כך האתחול כתוב במקום אחד.A short constructor hands the work to the full one with this(title, "unknown"). That way the setup lives in one place.נושאTopic 4
הבנאי הריק נעלםThe empty constructor disappears
הבנאי הריק נעלםThe empty constructor disappears
- ברגע שכתבתם
Book(String title), Java כבר לא נותנת Book().As soon as you write Book(String title), Java no longer gives you Book(). - javac עוצר ומסביר מה נדרש ומה נמצא.javac stops and explains what's required and what was found.
ברגע שכתבתם Book(String title), Java כבר לא נותנת Book(). javac עוצר ומסביר מה נדרש ומה נמצא.As soon as you write Book(String title), Java no longer gives you Book(). javac stops and explains what's required and what was found.נושאTopic 5
סדר האתחולInitialization order
סדר האתחולInitialization order
- כשהבנאי מתחיל,
a כבר 5 (האתחול שליד השדה רץ קודם), ו-b עדיין 0.When the constructor starts, a is already 5 (the initializer next to the field ran first), and b is still 0. - רק אחר כך הבנאי קובע את
b.Only then does the constructor set b.
כשהבנאי מתחיל, a כבר 5 (האתחול שליד השדה רץ קודם), ו-b עדיין 0. רק אחר כך הבנאי קובע את b.When the constructor starts, a is already 5 (the initializer next to the field ran first), and b is still 0. Only then does the constructor set b.נושאTopic 6
טעויות נפוצותCommon mistakes
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
constructor Book in class Book cannot be applied to given types;:constructor Book in class Book cannot be applied to given types;:
אלה הטעויות שהתרגילים מהסוג "צייד באגים" בנויים סביבן. כל אחת רצה באמת, והפלט הוא מה ש-Java עונה.These are the mistakes the "bug hunter" exercises are built around. Each one really runs, and the output is what Java says.נושאTopic 7
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 14: בנאים ל-BookShelf, step 14: Book constructors
ל-Book יש title, author, year, rating. בנאי מלא, ובנאי Book(String title, String author) שנותן שנה 0 ודירוג 3. line() מחזירה Dune by Herbert (1965) rating 5, ולשנה 0 unknown year. הדפיסו את Dune ואת new Book("Notes", "Me").Book has title, author, year, rating. A full constructor, and Book(String title, String author) giving year 0 and rating 3. line() returns Dune by Herbert (1965) rating 5, and for year 0 unknown year. Print Dune and new Book("Notes", "Me").בנאי: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-new:constructor: A special "method" named after the class, with no return type, that runs on new:
המונחים של המפגשThis session's termsthis: הפניה לעצם הנוכחי.this: A reference to the current object.
המונחים של המפגשThis session's termsהסתרה: כשלפרמטר או למשתנה מקומי יש שם של שדה, הוא "מסתיר" את השדה.shadowing: When a parameter or local variable has a field's name, it "hides" the field.
המונחים של המפגשThis session's termsשרשור בנאים: this(...) בשורה הראשונה של בנאי קורא לבנאי אחר של אותה מחלקה.constructor chaining: this(...) on a constructor's first line calls another constructor of the same class.
המונחים של המפגשThis session's termsבנאי ברירת מחדל: אם לא כתבתם שום בנאי, Java נותנת בנאי ריק בלי פרמטרים.default constructor: If you wrote no constructor, Java gives an empty one with no parameters.
המונחים של המפגשThis session's termsסדר האתחול: ב-new:initialization order: On new:
המונחים של המפגשThis session's termsבנאי רץ ב-new ומקבל ערכים.A constructor runs on new and takes values.
בנאי: אתחול בשורה אחתA constructor: setup in one linethis.title הוא השדה, title הוא הפרמטר.this.title is the field, title is the parameter.
בנאי: אתחול בשורה אחתA constructor: setup in one lineבזיכרון רואים את המסגרת new Book עם this שמצביע על העצם החדש.In memory you see the new Book frame with this pointing at the new object.
בנאי: אתחול בשורה אחתA constructor: setup in one lineבנאי קצר מעביר את העבודה לבנאי המלא עם this(title, "unknown").A short constructor hands the work to the full one with this(title, "unknown").
כמה בנאים ו-this(...)Several constructors and this(...)כך האתחול כתוב במקום אחד.That way the setup lives in one place.
כמה בנאים ו-this(...)Several constructors and this(...)ברגע שכתבתם Book(String title), Java כבר לא נותנת Book().As soon as you write Book(String title), Java no longer gives you Book().
הבנאי הריק נעלםThe empty constructor disappearsjavac עוצר ומסביר מה נדרש ומה נמצא.javac stops and explains what's required and what was found.
הבנאי הריק נעלםThe empty constructor disappearsכשהבנאי מתחיל, a כבר 5 (האתחול שליד השדה רץ קודם), ו-b עדיין 0.When the constructor starts, a is already 5 (the initializer next to the field ran first), and b is still 0.
סדר האתחולInitialization orderרק אחר כך הבנאי קובע את b.Only then does the constructor set b.
סדר האתחולInitialization orderconstructor Book in class Book cannot be applied to given types;:constructor Book in class Book cannot be applied to given types;:
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java saysמדף, צעד 14: בנאים ל-BookShelf, step 14: Book constructors
הצעד של היום בפרויקט מדףToday's step in the Shelf projectמה חסר בכותרת של בנאי, לעומת מתודה רגילה?What's missing from a constructor's header, compared to a normal method?
- טיפוס החזרהA return type
- שםA name
- סוגרייםBrackets
- פרמטריםParameters
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.טיפוס החזרהA return type
נכון: לא void ולא שום טיפוס.Right: no void and no type at all.בבנאי Book(String title) { title = title; }, מה הערך של השדה title אחרי new Book("Dune")?In the constructor Book(String title) { title = title; }, what's the title field after new Book("Dune")?
"Dune""Dune"nullnull- שגיאת קומפילציהA compile error
"title""title"
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.nullnull
נכון. צריך this.title = title;.Right. You need this.title = title;.איפה מותר לכתוב this(...) בבנאי?Where may you write this(...) in a constructor?
- בכל מקוםAnywhere
- רק בשורה הראשונהOnly on the first line
- רק בשורה האחרונהOnly on the last line
- רק מחוץ לבנאיOnly outside a constructor
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.רק בשורה הראשונהOnly on the first line
נכון.Right.תלמיד עם בנאיA student with a constructor
- מחלקה
Student עם בנאי Student(String name, int grade) ומתודה String info() (`Noa:A class Student with a constructor Student(String name, int grade) and a method String info() (`Noa:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: הכותרת שנעלמהBug hunter: the vanishing title
- התוכנית מדפיסה
null במקום Dune, ולא קורסת.The program prints null instead of Dune, and doesn't crash.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.פיצה עם ברירת מחדלA pizza with a default
- מחלקה
Pizza עם size ו-topping.A class Pizza with size and topping.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: אין בנאי ריקBug hunter: no empty constructor
- התוכנית מהתזכורת לא מתקמפלת.The program from the refresher doesn't compile.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.משך זמןA duration
- מחלקה
Duration ששומרת int seconds.A class Duration storing int seconds.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מה למדנוWhat we learned
- בנאי: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-
new:constructor: A special "method" named after the class, with no return type, that runs on new: - בנאי רץ ב-
new ומקבל ערכים.A constructor runs on new and takes values. - בנאי קצר מעביר את העבודה לבנאי המלא עם
this(title, "unknown").A short constructor hands the work to the full one with this(title, "unknown"). - ברגע שכתבתם
Book(String title), Java כבר לא נותנת Book().As soon as you write Book(String title), Java no longer gives you Book(). - כשהבנאי מתחיל,
a כבר 5 (האתחול שליד השדה רץ קודם), ו-b עדיין 0.When the constructor starts, a is already 5 (the initializer next to the field ran first), and b is still 0. constructor Book in class Book cannot be applied to given types;:constructor Book in class Book cannot be applied to given types;:
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.