נבנה על ידיBuilt by Ariel Yohimovich

JavaJava
14
חלק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.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • בנאי: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-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.
  • בנאי רץ ב-new ומקבל ערכים.A constructor runs on new and takes values.
  • this.title הוא השדה, title הוא הפרמטר.this.title is the field, title is the parameter.
  • בנאי קצר מעביר את העבודה לבנאי המלא עם 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.
  • ברגע שכתבתם 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.
  • כשהבנאי מתחיל, 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.

מונחיםTerms

  • בנאיconstructor: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-new: Book(String title) { this.title = title; }.A special "method" named after the class, with no return type, that runs on new: Book(String title) { this.title = title; }.
  • thisthis: הפניה לעצם הנוכחי. this.title הוא השדה, ו-title לבד הוא הפרמטר אם יש פרמטר בשם הזה.A reference to the current object. this.title is the field, and plain title is 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;), ורק אז גוף הבנאי.On new: every field gets 0, null or false, 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 stays null.
  • בנאיconstructor: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-new: Book(String title) { this.title = title; }.A special "method" named after the class, with no return type, that runs on new: Book(String title) { this.title = title; }.
  • thisthis: הפניה לעצם הנוכחי. this.title הוא השדה, ו-title לבד הוא הפרמטר אם יש פרמטר בשם הזה.A reference to the current object. this.title is the field, and plain title is 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 stays null.
  • שרשור בנאים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;), ורק אז גוף הבנאי.On new: every field gets 0, null or false, 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 disappearsconstructor 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 constructorconstructor 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"); }.

נבנה על ידיBuilt by Ariel Yohimovich