חלקPart 3: OOP: היסודותOOP: the basics
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
מחלקות ועצמיםClasses & objects
מחלקה כתבנית ועצם כמופע, שדות ומתודות, new, והפניות בזיכרון (stack מול heap).
A class as a blueprint and an object as an instance, fields and methods, new, and references in memory (stack vs heap).
הנקודות החשובותKey points
- עצם: מופע של מחלקה:object: An instance of a class:
- שדה: משתנה שמוגדר בתוך מחלקה, מחוץ למתודות.field: A variable defined inside a class, outside the methods.
class Bookמתארת מה יש לכל ספר: שדות (title,pages) ומתודות (describe).class Bookdescribes what every book has: fields (title,pages) and methods (describe).- כל
new Book()יוצר עצם חדש עם שדות משלו.Everynew Book()makes a new object with its own fields. b = aמעתיק את החץ: שני המשתנים מצביעים על אותו ספר, ושינוי דרךbנראה דרךa.b = acopies the arrow: both variables point to the same book, and a change throughbshows througha.c = nullהוא חץ שלא מצביע לשום מקום.c = nullis an arrow pointing nowhere.- מערך חדש של עצמים מלא ב-
null.A new array of objects is full ofnull. - כשהלולאה מגיעה לתא הריק,
b.titleמנסה לקרוא שדה של כלום.When the loop reaches the empty cell,b.titletries to read a field of nothing. Cannot read field "title" because "b" is null:Cannot read field "title" because "b" is null:
מונחיםTerms
- מחלקה (class)class: תבנית שממנה יוצרים עצמים. ב-Java כל קוד נמצא בתוך מחלקה, ושם הקובץ חייב להיות זהה לשם המחלקה הציבורית.A blueprint for making objects. In Java all code lives inside a class, and the file name must match the public class's name.
- עצםobject: מופע של מחלקה:
new Book()יוצר עצם חדש ב-heap, עם עותק משלו של כל השדות.An instance of a class:new Book()creates a new object on the heap, with its own copy of every field. - שדהfield: משתנה שמוגדר בתוך מחלקה, מחוץ למתודות. כל עצם מחזיק ערך משלו:
a.title,b.title. שדה מספרי מתחיל ב-0, ושדה של עצם ב-null.A variable defined inside a class, outside the methods. Each object holds its own value:a.title,b.title. A number field starts at 0, an object field atnull. - newnew: יוצר עצם חדש ב-heap ומחזיר הפניה אליו:
Book b = new Book();.Creates a new object on the heap and returns a reference to it:Book b = new Book();. - heapheap: האזור בזיכרון שבו גרים העצמים. המשתנים ב-stack מחזיקים רק חצים (הפניות) אליהם.The memory area where objects live. Variables on the stack hold only arrows (references) to them.
- nullnull: הפניה שלא מצביעה על שום עצם. משתנה מסוג עצם שלא קיבל ערך, או תא חדש במערך של עצמים, הוא
null.A reference that points to no object. An object variable that got no value, or a new cell in an array of objects, isnull. - NullPointerExceptionNullPointerException: החריגה כשמנסים לגשת לשדה או למתודה דרך
null. Java 21 אומרת בדיוק מה היה null:Cannot read field "title" because "b" is null.The exception when you reach a field or method throughnull. Java 21 says exactly what was null:Cannot read field "title" because "b" is null. - עצםobject: מופע של מחלקה:
new Book()יוצר עצם חדש ב-heap, עם עותק משלו של כל השדות.An instance of a class:new Book()creates a new object on the heap, with its own copy of every field. - שדהfield: משתנה שמוגדר בתוך מחלקה, מחוץ למתודות. כל עצם מחזיק ערך משלו:
a.title,b.title. שדה מספרי מתחיל ב-0, ושדה של עצם ב-null.A variable defined inside a class, outside the methods. Each object holds its own value:a.title,b.title. A number field starts at 0, an object field atnull. - newnew: יוצר עצם חדש ב-heap ומחזיר הפניה אליו:
Book b = new Book();.Creates a new object on the heap and returns a reference to it:Book b = new Book();. - heapheap: האזור בזיכרון שבו גרים העצמים. המשתנים ב-stack מחזיקים רק חצים (הפניות) אליהם.The memory area where objects live. Variables on the stack hold only arrows (references) to them.
- nullnull: הפניה שלא מצביעה על שום עצם. משתנה מסוג עצם שלא קיבל ערך, או תא חדש במערך של עצמים, הוא
null.A reference that points to no object. An object variable that got no value, or a new cell in an array of objects, isnull. - NullPointerExceptionNullPointerException: החריגה כשמנסים לגשת לשדה או למתודה דרך
null. Java 21 אומרת בדיוק מה היה null:Cannot read field "title" because "b" is null.The exception when you reach a field or method throughnull. Java 21 says exactly what was null:Cannot read field "title" because "b" is null.
Cheatsheet
מחלקה היא תבנית, עצם הוא מופעA class is a blueprint, an object is an instance
Book a = new Book();
a.title = "Dune";
a.pages = 412;
Book b = new Book();
b.title = "Emma";
b.pages = 474;
a.describe();
b.describe();משתנה מחזיק הפניה, לא עצםA variable holds a reference, not an object
Book a = new Book();
a.title = "Dune";
Book b = a;
b.title = "Dune Messiah";
Book c = null;
System.out.println(a.title + " | " + (c == null));שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
NullPointerException | NullPointerExceptionNullPointerException | Cannot read field "title" because "b" is null בשורה 7: בסיבוב השני b הוא shelf[1], שאף אחד לא שם בו ספר.Cannot read field "title" because "b" is null on line 7: in the second round b is shelf[1], where nobody put a book. |
NullPointerException | צייד באגים: מדף חצי ריקBug hunter: a half-empty shelf | Cannot read field "title" because "b" is null: shelf[1] אף פעם לא קיבל ספר. בודקים b != null לפני שניגשים לשדה.Cannot read field "title" because "b" is null: shelf[1] never got a book. Check b != null before reaching the field. |