חלקPart 3: OOP: היסודותOOP: the basics
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
static, final וקבועיםstatic, final & constants
שדות static משותפים, final למשתנים, לשדות ולמחלקות, וקבועים בשמות גדולים.
Shared static fields, final for variables, fields and classes, and constants in capital letters.
הנקודות החשובותKey points
- הפניה final:
final int[] aאומר שאי אפשר להצביע עםaעל מערך אחר.final reference:final int[] ameansacan't be pointed at another array. - מספר קסם: מספר שמופיע בקוד בלי הסבר (
0.17,56).magic number: A number in the code with no explanation (0.17,56). - משתנה final מקבל ערך פעם אחת.A final variable gets a value once.
- ניסיון לשנות אותו נעצר בקומפילציה.Trying to change it is stopped at compile time.
MIN_RATINGו-MAX_RATINGהם קבועים של המחלקה.MIN_RATINGandMAX_RATINGare class constants.countהוא מונה static אחד, ו-idהוא שדהfinalשל כל ספר, שנקבע פעם אחת בבנאי.countis one static counter, andidis afinalfield of each book, set once in the constructor.final int[] scoresנועל את החץ, לא את המערך:scores[0] = 95מותר, אבלscores = new int[3]היה נעצר בקומפילציה.final int[] scoreslocks the arrow, not the array:scores[0] = 95is allowed, butscores = new int[3]would be stopped at compile time.cannot assign a value to final variable count:cannot assign a value to final variable count:- מדף, צעד 16: מונה ספרים וקבועי דירוגShelf, step 16: a book counter and rating constants
מונחיםTerms
- finalfinal: משתנה
finalמקבל ערך פעם אחת ולא משתנה יותר. קבועים כותבים באותיות גדולות:final int MAX = 10;.Afinalvariable gets a value once and never changes. Constants are written in capitals:final int MAX = 10;. - הפניה finalfinal reference:
final int[] aאומר שאי אפשר להצביע עםaעל מערך אחר. את התוכן של המערך עדיין אפשר לשנות.final int[] ameansacan't be pointed at another array. The array's contents can still change. - קבועconstant:
static final: ערך אחד למחלקה שאי אפשר לשנות. נהוג לכתוב באותיות גדולות:static final double VAT = 0.17;.static final: one value per class that can't change. Usually written in capitals:static final double VAT = 0.17;. - מספר קסםmagic number: מספר שמופיע בקוד בלי הסבר (
0.17,56). קבוע עם שם (VAT,PASS) מסביר את עצמו ומשתנה במקום אחד.A number in the code with no explanation (0.17,56). A named constant (VAT,PASS) explains itself and changes in one place. - חבר staticstatic member: שדה או מתודה ששייכים למחלקה עצמה, בעותק אחד:
Ticket.count,Math.max. לא צריך עצם כדי להשתמש בהם.A field or method that belongs to the class itself, in one copy:Ticket.count,Math.max. No object is needed to use them. - מתודת יצרןfactory method: מתודה static שמחזירה עצם חדש, עם שם שמסביר מה מקבלים:
Temperature.ofCelsius(100). לעתים קרובות הבנאי עצמו private.A static method that returns a new object, with a name explaining what you get:Temperature.ofCelsius(100). Often the constructor itself is private. - מחלקת עזרutility class: מחלקה שכל המתודות שלה static, כמו
Mathו-Arrays: אוסף כלים, בלי מצב משלה.A class whose methods are all static, likeMathandArrays: a toolbox with no state of its own. - הפניה finalfinal reference:
final int[] aאומר שאי אפשר להצביע עםaעל מערך אחר. את התוכן של המערך עדיין אפשר לשנות.final int[] ameansacan't be pointed at another array. The array's contents can still change. - מספר קסםmagic number: מספר שמופיע בקוד בלי הסבר (
0.17,56). קבוע עם שם (VAT,PASS) מסביר את עצמו ומשתנה במקום אחד.A number in the code with no explanation (0.17,56). A named constant (VAT,PASS) explains itself and changes in one place. - מתודת יצרןfactory method: מתודה static שמחזירה עצם חדש, עם שם שמסביר מה מקבלים:
Temperature.ofCelsius(100). לעתים קרובות הבנאי עצמו private.A static method that returns a new object, with a name explaining what you get:Temperature.ofCelsius(100). Often the constructor itself is private.
Cheatsheet
קבועים ומונה מזהיםConstants and an id counter
Book a = new Book("Dune", 5);
Book b = new Book("Emma", 4);
System.out.println(a.getId() + " " + b.getId() + " of " + Book.getCount());final על הפניהfinal on a reference
final int[] scores = {70, 80};
scores[0] = 95;
System.out.println(Arrays.toString(scores));שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
cannot assign a value to final variable max | final: השמה אחת בלבדfinal: one assignment only | cannot assign a value to final variable max: max כבר קיבל 3.cannot assign a value to final variable max: max already got 3. |
cannot assign a value to final variable count | צייד באגים: מונה שאסור לו לזוזBug hunter: a counter that can't move | cannot assign a value to final variable count: מונה חייב להשתנות, ולכן הוא לא יכול להיות final.cannot assign a value to final variable count: a counter must change, so it can't be final. |