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

חזרה למפגשBack
JavaJava

מחלקות ועצמיםClasses & objects

מפגש 13: מחלקה כתבנית ועצם כמופע, שדות ומתודות, new, והפניות בזיכרון (stack מול heap).Session 13: A class as a blueprint and an object as an instance, fields and methods, new, and references in memory (stack vs heap).

מה נעשה היוםToday's plan

  • מונחיםTerms
  • מחלקה היא תבנית, עצם הוא מופעA class is a blueprint, an object is an instance
  • משתנה מחזיק הפניה, לא עצםA variable holds a reference, not an object
  • NullPointerExceptionNullPointerException
  • טעויות נפוצותCommon mistakes
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • עצם: מופע של מחלקה:object: An instance of a class:
  • שדה: משתנה שמוגדר בתוך מחלקה, מחוץ למתודות.field: A variable defined inside a class, outside the methods.
  • new: יוצר עצם חדש ב-heap ומחזיר הפניה אליו:new: Creates a new object on the heap and returns a reference to it:
  • heap: האזור בזיכרון שבו גרים העצמים.heap: The memory area where objects live.
  • null: הפניה שלא מצביעה על שום עצם.null: A reference that points to no object.
  • NullPointerException: החריגה כשמנסים לגשת לשדה או למתודה דרך null.NullPointerException: The exception when you reach a field or method through null.
נושאTopic 2

מחלקה היא תבנית, עצם הוא מופעA class is a blueprint, an object is an instance

מחלקה היא תבנית, עצם הוא מופעA class is a blueprint, an object is an instance

  • class Book מתארת מה יש לכל ספר: שדות (title, pages) ומתודות (describe).class Book describes what every book has: fields (title, pages) and methods (describe).
  • כל new Book() יוצר עצם חדש עם שדות משלו.Every new Book() makes a new object with its own fields.
  • בתוך describe, title הוא השדה של העצם שעליו קראו למתודה.Inside describe, title is the field of the object the method was called on.
נושאTopic 3

משתנה מחזיק הפניה, לא עצםA variable holds a reference, not an object

משתנה מחזיק הפניה, לא עצםA variable holds a reference, not an object

  • b = a מעתיק את החץ: שני המשתנים מצביעים על אותו ספר, ושינוי דרך b נראה דרך a.b = a copies the arrow: both variables point to the same book, and a change through b shows through a.
  • c = null הוא חץ שלא מצביע לשום מקום.c = null is an arrow pointing nowhere.
נושאTopic 4

NullPointerExceptionNullPointerException

NullPointerExceptionNullPointerException

  • מערך חדש של עצמים מלא ב-null.A new array of objects is full of null.
  • כשהלולאה מגיעה לתא הריק, b.title מנסה לקרוא שדה של כלום.When the loop reaches the empty cell, b.title tries to read a field of nothing.
  • Java 21 אומרת בדיוק מה היה null.Java 21 says exactly what was null.
נושאTopic 5

טעויות נפוצותCommon mistakes

טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says

  • Cannot read field "title" because "b" is null:Cannot read field "title" because "b" is null:
נושאTopic 6

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 13: המחלקה BookShelf, step 13: the Book class

עצם: מופע של מחלקה:object: An instance of a class:

שדה: משתנה שמוגדר בתוך מחלקה, מחוץ למתודות.field: A variable defined inside a class, outside the methods.

new: יוצר עצם חדש ב-heap ומחזיר הפניה אליו:new: Creates a new object on the heap and returns a reference to it:

heap: האזור בזיכרון שבו גרים העצמים.heap: The memory area where objects live.

null: הפניה שלא מצביעה על שום עצם.null: A reference that points to no object.

NullPointerException: החריגה כשמנסים לגשת לשדה או למתודה דרך null.NullPointerException: The exception when you reach a field or method through null.

class Book מתארת מה יש לכל ספר: שדות (title, pages) ומתודות (describe).class Book describes what every book has: fields (title, pages) and methods (describe).

כל new Book() יוצר עצם חדש עם שדות משלו.Every new Book() makes a new object with its own fields.

בתוך describe, title הוא השדה של העצם שעליו קראו למתודה.Inside describe, title is the field of the object the method was called on.

b = a מעתיק את החץ: שני המשתנים מצביעים על אותו ספר, ושינוי דרך b נראה דרך a.b = a copies the arrow: both variables point to the same book, and a change through b shows through a.

c = null הוא חץ שלא מצביע לשום מקום.c = null is an arrow pointing nowhere.

מערך חדש של עצמים מלא ב-null.A new array of objects is full of null.

כשהלולאה מגיעה לתא הריק, b.title מנסה לקרוא שדה של כלום.When the loop reaches the empty cell, b.title tries to read a field of nothing.

Java 21 אומרת בדיוק מה היה null.Java 21 says exactly what was null.

Cannot read field "title" because "b" is null:Cannot read field "title" because "b" is null:

מדף, צעד 13: המחלקה BookShelf, step 13: the Book class

כמה עצמים יש בזיכרון אחרי הקוד?How many objects are in memory after this code?

  • 11
  • 22
  • 33
  • 00

22

מה הערך של שדה String title בעצם חדש שאף אחד לא נתן לו ערך?What's a String title field in a new object that nobody gave a value?

  • """"
  • nullnull
  • שגיאת קומפילציהA compile error
  • "title""title"

nullnull

a ו-b מצביעים על אותו ספר. מה מודפס?a and b point to the same book. What's printed?

  • XX
  • השם הקודםThe old title
  • nullnull
  • שגיאהAn error

XX

נקודהA point

  • כתבו מחלקה Point עם שדות int x, int y ומתודה double distance() שמחזירה את המרחק מראשית הצירים (Math.sqrt(x*x + y*y)).Write a class Point with fields int x, int y and a method double distance() returning the distance from the origin (Math.sqrt(x*x + y*y)).

שני כלביםTwo dogs

  • מחלקה Dog עם name, age, מתודה String describe() שמחזירה למשל Rex is 3, ומתודה void birthday() שמעלה את הגיל.A class Dog with name, age, a method String describe() returning e.g.

חשבון בנקA bank account

  • מחלקה Account עם int balance, void deposit(int amount) ו-boolean withdraw(int amount), שמושכת רק אם יש מספיק ומחזירה האם הצליחה.A class Account with int balance, void deposit(int amount) and boolean withdraw(int amount), which withdraws only if there's enough and returns whether it worked.

צייד באגים: מדף חצי ריקBug hunter: a half-empty shelf

  • התוכנית מהתזכורת קורסת עם NullPointerException.The program from the refresher crashes with NullPointerException.

לשנות או להחליף?Change or replace?

  • rename(book) משנה את השדה של הספר שקיבלה.rename(book) changes the field of the book it got.

מדף, צעד 13: המחלקה BookShelf, step 13: the Book class

  • מחלקה Book עם title, author, year, ומתודות String line() (Emma by Austen (1815)) ו-boolean isClassic() (לפני 1950).A class Book with title, author, year, and methods String line() (Emma by Austen (1815)) and boolean isClassic() (before 1950).

יומן טמפרטורותA temperature log

  • מחלקה TempLog עם מערך double[] values = new double[10], מונה int size, ומתודות add, max ו-average (רק על התאים שמולאו).A class TempLog with an array double[] values = new double[10], a counter int size, and methods add, max and average (over the filled cells only).

מלבן שיודע לגדולA rectangle that can grow

  • מחלקה Rect עם width, height ומתודות area(), perimeter(), isSquare() ו-Rect scaled(int k) שמחזירה מלבן חדש גדול פי k (המקורי לא משתנה).A class Rect with width, height and methods area(), perimeter(), isSquare() and Rect scaled(int k) returning a new rectangle k times bigger (the original doesn't change).

מה למדנוWhat we learned

  • עצם: מופע של מחלקה:object: An instance of a class:
  • class Book מתארת מה יש לכל ספר: שדות (title, pages) ומתודות (describe).class Book describes what every book has: fields (title, pages) and methods (describe).
  • b = a מעתיק את החץ: שני המשתנים מצביעים על אותו ספר, ושינוי דרך b נראה דרך a.b = a copies the arrow: both variables point to the same book, and a change through b shows through a.
  • מערך חדש של עצמים מלא ב-null.A new array of objects is full of null.
  • Cannot read field "title" because "b" is null:Cannot read field "title" because "b" is null:
  • מדף, צעד 13: המחלקה BookShelf, step 13: the Book class

עכשיו לתרגולNow, practice

6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

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