Skip to content · ⁨דלג לתוכן⁩

Class Creation · ⁨יצירת מחלקות⁩

AP Computer Science A · ⁨מדעי מחשב A - AP⁩ · Topic 3 · ⁨נושא 3⁩

Video lesson for this topic · ⁨שיעור וידאו לנושא זה⁩ Open the video page · ⁨פתח את עמוד הוידאו⁩
7:26

יצירת מחלקות

הנה סטודנט, מאוחסן כשלושה משתנים נפרדים: שם, שנה ומדורג. זה עובד. כעת הוסף אלפי סטודנטים. תצטרך שלושת אלפים משתנים…

English narration · English + 中文 subtitles burned in · ⁨קריאת קול באנגלית · תרגום אנגלי + סינית שרוף בתוך הסרטון⁩

3.1

Abstraction and Program Design · ⁨שכלול ועיצוב תוכנה⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 3.1.A: Represent the design of a program by using natural language or creating diagrams that indicate the classes in the program and the data and procedural abstractions found in each class by including all attributes and behaviors.

  • 3.1.A.1 Abstraction is the process of reducing complexity by focusing on the main idea. By hiding details irrelevant to the question at hand and bringing together related and useful details, abstraction reduces complexity and allows one to focus on the idea.
  • 3.1.A.2 Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation. Data abstraction manages complexity by giving data a name without referencing the specific details of the representation. Data can take the form of a single variable or a collection of data, such as in a class or a set of data.
  • 3.1.A.3 An attribute is a type of data abstraction that is defined in a class outside any method or constructor. An instance variable is an attribute whose value is unique to each instance of the class. A class variable is an attribute shared by all instances of the class.
  • 3.1.A.4 Procedural abstraction provides a name for a process and allows a method to be used only knowing what it does, not how it does it. Through method decomposition, a programmer breaks down larger behaviors of the class into smaller behaviors by creating methods to represent each individual smaller behavior. A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for code reuse, which helps manage complexity.
  • 3.1.A.5 Using parameters allows procedures to be generalized, enabling the procedures to be reused with a range of input values or arguments.
  • 3.1.A.6 Using procedural abstraction in a program allows programmers to change the internals of a method (to make it faster, more efficient, use less storage, etc.) without needing to notify method users of the change as long as the method signature and what the method does is preserved.
  • 3.1.A.7 Prior to implementing a class, it is helpful to take time to design each class including its attributes and behaviors. This design can be represented using natural language or diagrams.
עברית

מטרות למידה 3.1.A: ייצוג עיצוב תוכנית באמצעות שפה טבעית או ליצירת דיאגרמות המציינות את הכיתות בתוכנית ואת השתקות הנתונים והשלכות המצויות בכל כיתה על ידיכלול כל האטריביוטים וההתנהגויות.

  • 3.1.A.1 השתקה היא תהליך של הפחתת מורכבות על ידי התמקדות ברעיון המרכזי. על ידי הסתרת פרטים שאינם רלוונטיים לשאלה הקיימת וקיבוצי פרטים קשורים ומفيدים, השתקה מפחיתה מורכבות ומאפשרת להתמקד ברעיון.
  • 3.1.A.2 השתקת נתונים מספקת הפרדה בין התכונות המופשטות של סוג נתונים לבין הפרטים הממשיים של ההצגה שלה. השתקת נתונים מנהלת מורכבות על ידי מתן שם לנתונים ללא הייחוס לפרטים הספציפיים של ההצגה. הנתונים יכולים לקבל צורה של משתנה בודד או אוסף נתונים, כגון בכיתה או בערכת נתונים.
  • 3.1.A.3 אטריביוט הוא סוג של השתקת נתונים המוגדרת בכיתה מחוץ לכל שיטה או בונה. משתנה אובייקט הוא אטריביוט שערכו ייחודי לכל אובייקט של הכיתה. משתנה כיתה הוא אטריביוט המשותף לכל האובייקטים של הכיתה.
  • 3.1.A.4 השתקה של שלבים מספקת שם לתהליך ומאפשרת לשיטה להיות מופעלת רק כאשר יודעים מה היא עושה, לא כיצד היא עושה זאת. באמצעות פירוק שיטות, מתכנת מפרק התנהגויות גדולות יותר של הכיתה להתנהגויות קטנות יותר על ידי יצירת שיטות המייצגות כל התנהגות קטנה בודדת. השתקת שלבים עשויה לחלץ מאפיינים משותפים כדי לגנרלize פונקציונליות במקום להכפיל קוד. זה מאפשר שימוש חוזר בקוד, שמסייע בניהול מורכבות.
  • 3.1.A.5 השימוש בפארמטרים מאפשר גנרלזציה של הליכים, ומאפשר לשימוש חוזר בהם עם מגוון ערכי כניסה או ארגומנטים.
  • 3.1.A.6 השימוש בהשתקת שלבים בתוכנית מאפשר למתכנתים לשנות את הפנים של שיטה (כדי להפכה מהירה יותר, יעילה יותר, לצרוך פחות אחסון, וכו') ללא הצורך בהודעת משתמשי השיטה על השינוי כל עוד חתימת השיטה ומה שהשיטה עושה נשמרים.
  • 3.1.A.7 לפני מימוש כיתה, מומלץ לקחת זמן לעצב כל כיתה כולל את האטריביוטים וההתנהגויות שלה. עיצוב זה ניתן לייצוג באמצעות שפה טבעית או דיאגרמות.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

Abstraction 抽象 means hiding detail behind a simple interface – you use a String without knowing how it stores characters. Good design breaks a problem into classes, each responsible for one idea. This topic is about writing your own classes.

עברית
פאזל בתהליך: מחלקות ומתודות הן חלקים מודולריים לעיצוב תוכנה גדול יותר
פאזל בתהליך: מחלקות ומתודות הן חלקים מודולריים לעיצוב תוכנה גדול יותר

שכלול אומר להסתיר פרטים מאחורי ממשק פשוט – אתם משתמשים ב-String מבלי לדעת כיצד הוא מאחסן תוויות. עיצוב טוב מפצל בעיה למחלקות, שכל אחת אחראית על רעיון אחד. נושא זה עוסק בכתיבת המחלקות שלך.

פירוק תוכנה למודולים ותת-מודולים
פירוק תוכנית למודולים ותת-מודולים
3.2

The Impact of Program Design · ⁨השפעת עיצוב התוכנה⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 3.2.A: Explain the social and ethical implications of computing systems.

  • 3.2.A.1 System reliability refers to the program being able to perform its tasks as expected under stated conditions without failure. Programmers should make an effort to maximize system reliability by testing the program with a variety of conditions.
  • 3.2.A.2 The creation of programs has impacts on society, the economy, and culture. These impacts can be both beneficial and harmful. Programs meant to fill a need or solve a problem can have unintended harmful effects beyond their intended use.
  • 3.2.A.3 Legal issues and intellectual property concerns arise when creating programs. Programmers often reuse code written by others and published as open source and free to use. Incorporation of code that is not published as open source requires the programmer to obtain permission and often purchase the code before integrating it into their program.
עברית

מטרות למידה 3.2.A: הסבר על ההשלכות החברתיות והאתיות של מערכות מחשוב.

  • 3.2.A.1 אמינות מערכת מתייחסת ליכולת התוכנית לבצע את משימותיה כפי שצפוי בתנאים המפורשים ללא כישלון. מתכנתים צריכים להשקיע מאמץ במקסום אמינות המערכת על ידי בדיקת התוכנית עם מגוון תנאים שונים.
  • 3.2.A.2 יצירת תוכניות משפיעה על החברה, הכלכלה והתרבות. השפעות אלו יכולות להיות גם חיוביות וגם שליליות. תוכניות שמיועדות למלא צורך או לפתור בעיה עשויות לגרום להשלכות מזיקות לא רצויות מעבר לשימוש הנכון שלהן.
  • 3.2.A.3 נושאים משפטיים ודאגות בנוגע לזכויות יוצרים ועליון אינטלקטואלי נובעים במהלך יצירת תוכניות. מתכנתים לעיתים קרובות משתמשים מחדש בקוד שנכתב על ידי אחרים ופורסם כקוד פתוח ומחופשי לשימוש. שימוש בקוד שאינו פורסם כקוד פתוח דורש מהמתכנת לקבל רשות ולעיתים קרובות גם לרכוש את הקוד לפני שמשלב אותו בתוכנית שלו.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

Design choices affect whether code is correct, readable, and reusable. Encapsulation 封装 – keeping data private and exposing it only through methods – protects an object's state from misuse and lets you change the inside without breaking users of the class. Thoughtful naming, single-purpose methods, and testing reduce bugs.

Design also carries responsibility beyond the code. System reliability 系统可靠性 - a program performing its tasks as expected, without failure - is something programmers should maximise through careful design and testing. Programs have real impacts on society, the economy, and culture that can be both beneficial and harmful. And creating programs raises legal and intellectual-property 知识产权 concerns: programmers often reuse code published as open source 开源 and free to use, but must respect its licence and give credit rather than copy others' work as their own.

עברית

בחירות עיצוב משפיעות על כך שהקוד יהיה נכון, קריא ושימוש-מחדש. אקספולציה – שמירת נתונים כפרטיים וחשיפתם רק דרך מתודות – מגנה על מצב האובייקט משימוש לא תקין ומאפשרת לשנות את הפנים מבלי לפגוע במשתמשי המחלקה. סימון מחושב, מתודות בעלות מטרה אחת ובדיקות מקטינים באגים.

העיצוב כולל גם אחריות מעבר לקוד. אמינות מערכת - ביצוע תוכנית למשימותיה בצורה הצפויה, ללא כשל - הוא משהו שתוכנתנים צריכים למקסם דרך עיצוב ובדיקה קפדניים. לתוכניות יש השפעות אמיתיות על החברה, הכלכלה והתרבות שיכולות להיות גם מועילות וגם מזיקות. ויצירת תוכניות מעלה דאגות משפטיות ורכושיות: תוכנתנים לעיתים קרובות משתמשים מחדש בקוד שפורסם כ-code פתוח וזמין בחינם, אך עליהם לכבד את הרישיון שלו ולהעריך זכות במקום להעתיק עבודת אחרים כשלהם.

3.3

The Anatomy of a Class · ⁨אנטומיה של מחלקה⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 3.3.A: Develop code to designate access and visibility constraints to classes, data, constructors, and methods.

  • 3.3.A.1 Data encapsulation is a technique in which the implementation details of a class are kept hidden from external classes. The keywords public and private affect the access of classes, data, constructors, and methods. The keyword private restricts access to the declaring class, while the keyword public allows access from classes outside the declaring class.
  • 3.3.A.2 In this course, classes are always designated public and are declared with the keyword class.
  • 3.3.A.3 In this course, constructors are always designated public.
  • 3.3.A.4 Instance variables belong to the object, and each object has its own copy of the variable.
  • 3.3.A.5 Access to attributes should be kept internal to the class in order to accomplish encapsulation. Therefore, it is good programming practice to designate the instance variables for these attributes as private unless the class specification states otherwise.
  • 3.3.A.6 Access to behaviors can be internal or external to the class. Methods designated as public can be accessed internally or externally to a class, whereas methods designated as private can only be accessed internally to the class.
עברית

מטרות למידה 3.3.A: פיתוח קוד כדי לקבוע מגבלות גישה ונראות עבור מחלקות, נתונים, בוני-בנייה (constructors) ומתודות.

  • 3.3.A.1 עיטוף נתונים היא טכניקה בה פרטי היישום של מחלקה נשמרים בסתר ממחלקות חיצוניות. המילים public וprivate משפיעות על הגישה למחלקות, נתונים, בוני-בנייה ומתודות. המילה private מגבילה גישה למחלקה המצהירה בלבד, בעוד שהמילה public מאפשרת גישה ממחלקות מחוץ למחלקה המצהרת.
  • 3.3.A.2 בקורס זה, מחלקות מסומנות תמיד כpublic ומוגדרות עם המילה class.
  • 3.3.A.3 בקורס זה, בוני-בנייה מסומנים תמיד כpublic.
  • 3.3.A.4 משתנים ממוחלקים (Instance variables) שייכים לאובייקט, ולכל אובייקט יש העתק משלו של המשתנה.
  • 3.3.A.5 גישה לתכונות (attributes) צריכה להיות מוגבלת בפנים המחלקה כדי להשגת עיטוף. לכן, זוהי שיטה תכנותית טובה לסמן את המשתנים הממוחלקים עבור תכונות אלו כprivate אלא אם כן שיתוף הפעולה של המחלקה קובע אחרת.
  • 3.3.A.6 גישה להתנהגויות יכולה להיות פנימית או חיצונית למחלקה. מתודות המסומנות כpublic ניתנות לגישה פנימית או חיצונית למחלקה, בעוד שמתודות המסומנות כprivate ניתנות לגישה רק בפנים המחלקה.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

A class has three parts: instance variables 实例变量 (fields – the object's data), constructors (build objects), and methods (behavior). Fields are usually private; methods are usually public:

עברית

למחלקה יש שלושה חלקים: משתני instance (שדות – הנתונים של האובייקט), constructors (בונים אובייקטים) ו-מתודות (התנהגות). שדות הם בדרך כלל private; מתודות הן בדרך כלל public:

תרשים מחלקה: מאפיינים פרטיים ושיטות ציבוריות
תרשים מחלקה: מאפיינים פרטיים ושיטות ציבוריות
public class Student {
    private String name;      // instance variable
    private int score;

    public Student(String n, int s) {   // constructor
        name = n;
        score = s;
    }
    public int getScore() { return score; }   // accessor
}
תוכנית: מחלקה היא תבנית המגדירה כיצד אובייקטים מסוג זה נבנים
תוכנית: מחלקה היא תבנית המגדירה כיצד אובייקטים מסוג זה נבנים
Explore · ⁨חקור⁩

See an object's fields as boxes · ⁨ראו שדות של אובייקת כתיבות⁩

A class groups related data (its fields) and methods. Each object gets its own set of field boxes; assigning to one changes that object only. · ⁨מחלקה מקבוצה נתונים קשורים (ה-שדות שלה) ומתודות. לכל אובייקת יש סט תיבות שדות משלו; שינוי בשדה של אחד משנה רק את האובייקת ההוא.⁩

Vocabulary · ⁨מילון מונחים⁩ Train · ⁨אימון⁩
English עברית
Abstraction/əbˈstrækʃn/ אבסטרקציה
Encapsulation/ɪnˌkæpsjʊˈleɪʃn/ עטיפה
System reliability/ˈsɪstəm rɪˌlaɪəˈbɪlɪti/ אמינות מערכת
legal and intellectual-property/ˈliːɡl ænd ˌɪntəˈlektʃuːəl ˈprɒpəti/ זכויות משפטיות ונכסי קניין רוחני
open source/ˈəʊpən sɔːs/ קוד פתוח
instance variables/ˈɪnstəns ˈveərɪəblz/ משתני אקספלן (Instance variables)
constructor/kənˈstrʌktə/ בונה
accessor (getter)/əkˈsesə/ שליף (getter)
3.4

Constructors

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 3.4.A: Develop code to declare instance variables for the attributes to be initialized in the body of the constructors of a class.

  • 3.4.A.1 An object's state refers to its attributes and their values at a given time and is defined by instance variables belonging to the object. This defines a has-a relationship between the object and its instance variables.
  • 3.4.A.2 A constructor is used to set the initial state of an object, which should include initial values for all instance variables. When a constructor is called, memory is allocated for the object and the associated object reference is returned. Constructor parameters, if specified, provide data to initialize instance variables.
  • 3.4.A.3 When a mutable object is a constructor parameter, the instance variable should be initialized with a copy of the referenced object. In this way, the instance variable does not hold a reference to the original object, and methods are prevented from modifying the state of the original object.
  • 3.4.A.4 When no constructor is written, Java provides a no-parameter constructor, and the instance variables are set to default values according to the data type of the attribute. This constructor is called the default constructor.
  • 3.4.A.5 The default value for an attribute of type int is 0. The default value of an attribute of type double is 0.0. The default value of an attribute of type boolean is false. The default value of a reference type is null.
עברית

מטרות למידה 3.4.A: פיתוח קוד כדי להצהיר משתנים ממוחלקים עבור התכונות שיועדים להתחיל בשיקוף בוני-הבנייה של מחלקה.

  • 3.4.A.1 מצב (state) של אובייקט מתייחס לתכונותיו וערכיהן בזמן נתון, והוא מוגדר על ידי משתנים ממוחלקים השייכים לאובייקט. הדבר מגדיר יחס "יש-א-" (has-a) בין האובייקט לבין המשתנים הממוחלקים שלו.
  • 3.4.A.2 בונה-בנייה משמש להגדרת המצב ההתחלתי של אובייקט, הכולל ערכים התחלתיים לכל המשתנים הממוחלקים. כאשר בונה-בנייה נקרא, זיכרון מוקצה עבור האובייקט והערות האובייקט הקשורה מוחזרת. פרמטרי בונה-הבנייה, אם מצוינים, מספקים נתונים להתחלת משתנים ממוחלקים.
  • 3.4.A.3 כאשר אובייקט משתנה (mutable object) הוא פרמטר בונה-בנייה, המשתנה הממוחלק צריך להתחיל בהעתק של האובייקט המצויין. באופן זה, המשתנה הממוחלק אינו מחזיק ברפרנס לאובייקט המקורי, ומתודות מונעות משינוי המצב של האובייקט המקורי.
  • 3.4.A.4 כאשר אין כתוב בונה-בנייה, Java מספקת בונה-בנייה ללא פרמטרים, והמשתנים הממוחלקים מוגדרים לערכים ברירת מחדל בהתאם לסוג הנתונים של התכונה. בונה זה מכונה בונה-בנייה ברירת מחדל.
  • 3.4.A.5 הערך הרירת מחדל לתכונה מסוג int הוא 0. הערך ברירת מחדל לתכונה מסוג double הוא 0.0. הערך ברירת מחדל לתכונה מסוג boolean הוא false. הערך ברירת מחדל לסוג עיטוף (reference type) הוא null.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

A constructor 构造函数 has the same name as the class and no return type. It runs when you write new, and its job is to initialize the fields. A class can have several constructors with different parameter lists (overloading 重载); a no-argument constructor sets defaults.

עברית

Constructor יש לו אותו שם כמו המחלקה ואין לו סוג החזרה. הוא רץ כאשר אתם כותבים new, ומשימתו היא לאיפוס השדות. למחלקה יכול להיות מספר several constructors עם רשימות פרמטרים שונות (overloading); constructor ללא פרמטרים קובע ברירת מחדל.

Vocabulary · ⁨מילון מונחים⁩ Train · ⁨אימון⁩
English עברית
overloading/ˌəʊvəˈləʊdɪŋ/ עמיסה (Overloading)
3.5

Methods: How to Write Them · ⁨שיטות: כיצד לכתוב אותן⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 3.5.A: Develop code to define behaviors of an object through methods written in a class using primitive values and determine the result of calling these methods.

  • 3.5.A.1 A void method does not return a value. Its header contains the keyword void before the method name.
  • 3.5.A.2 A non-void method returns a single value. Its header includes the return type in place of the keyword void.
  • 3.5.A.3 In non-void methods, a return expression compatible with the return type is evaluated, and the value is returned. This is referred to as return by value.
  • 3.5.A.4 The return keyword is used to return the flow of control to the point where the method or constructor was called. Any code that is sequentially after a return statement will never be executed. Executing a return statement inside a selection or iteration statement will halt the statement and exit the method or constructor.
  • 3.5.A.5 An accessor method allows objects of other classes to obtain a copy of the value of instance variables or class variables. An accessor method is a non-void method.
  • 3.5.A.6 A mutator (modifier) method is a method that changes the values of the instance variables or class variables. A mutator method is often a void method.
  • 3.5.A.7 Methods with parameters receive values through those parameters and use those values in accomplishing the method's task.
  • 3.5.A.8 When an argument is a primitive value, the parameter is initialized with a copy of that value. Changes to the parameter have no effect on the corresponding argument.
עברית

מטרות למידה 3.5.A: פיתוח קוד להגדרת התנהגויות של אובייקט באמצעות שיטות שנכתבו בכיתה, תוך שימוש בערכים ראשוניים, וקביעת התוצאה של קריאת שיטות אלו.

  • 3.5.A.1 שיטה void אינה מחזירה ערך. החוליה שלה מכילה את המפתח void לפני שם השיטה.
  • 3.5.A.2 מתודה שאינה void מחזירה ערך יחיד. הכותרת שלה כוללת את סוג ההחזרה במקום המילת המפתח void.
  • 3.5.A.3 במתודות שאינן void, ביטוי החזרה תואם לסוג ההחזרה מוערך והערך מוחזר. דבר זה מכונה החזרה בערך (return by value).
  • 3.5.A.4 המפתח return משמש להחזרת זרימת הביצועים לנקודה שבה הושעלה השיטה או הקונסטרוקטור. כל קוד הנמצא לאחר פקודת return לא יופעל לעולם. ביצוע פקודת return בתוך ביטוי בחירה או חזרה יפסיק את הביטוי ויצא מהשיטה או מהקונסטרוקטור.
  • 3.5.A.5 מתודת גישה מאפשרת לאובייקטים ממחלקות אחרות לקבל העתק של ערך המשתנים האינסטנציה או משתני המחלקה. מתודת גישה היא מתודה שאינה void.
  • 3.5.A.6 מתודת שינוי (מודיפייר) היא מתודה המשנה את ערכי המשתנים האינסטנציה או משתני המחלקה. מתודת שינוי היא לעיתים קרובות מתודה void.
  • 3.5.A.7 שיטות עם פרמטרים מקבלות ערכים דרך פרמטרים אלו ומשתמשות בערכים אלו כדי לבצע את משימת השיטה.
  • 3.5.A.8 כאשר הארגומנט הוא ערך ראשוני, הפרמטר מצופיין בהעתק של הערך הזה. שינויים בפרמטר אינם משפיעים על הארגומנט המתאים.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

A method has a signature, a return type, and a body. An accessor (getter) 访问器 returns information without changing the object; a mutator (setter) 修改器 changes a field. A method returning a value must have a return of the right type on every path; a void method returns nothing.

עברית

לשיטה יש חתימה, סוג החזרה וגוף. אקססור (Getter) מחזיר מידע מבלי לשנות את האובייקט; מיוטטור (Setter) משנה שדה. לשיטה שמחזירה ערך חייב להיות return מהסוג הנכון בכל מסלול; שיטת void לא מחזירה כלום.

public void setScore(int s) { score = s; }   // mutator
public String toString() { return name + ": " + score; }
Explore · ⁨חקור⁩

Follow a method call and its return · ⁨עקוב אחר קריאת מתודה והחזרת הערך⁩

Calling a method pushes a frame with its parameters; when it hits return, the frame pops and the value goes back to the caller. · ⁨כאשר קוראים מתודה, דוחפים מסגרת עם הפרמטרים שלה; כאשר היא מגיעה לreturn, המסגרת נדחסת והערך חוזר לקורא.⁩

Vocabulary · ⁨מילון מונחים⁩ Train · ⁨אימון⁩
English עברית
mutator (setter)/mjuːˈteɪtə/ מוטור (סטטר)
static (class) variable/ˈstætɪk ˈveərɪəbl/ משתנה סטטי (קלאסי)
3.6

Passing and Returning References of an Object · ⁨העברת והשבת רפרנסים של אובייקט⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 3.6.A: Develop code to define behaviors of an object through methods written in a class using object references and determine the result of calling these methods.

  • 3.6.A.1 When an argument is an object reference, the parameter is initialized with a copy of that reference; it does not create a new independent copy of the object. If the parameter refers to a mutable object, the method or constructor can use this reference to alter the state of the object. It is good programming practice to not modify mutable objects that are passed as parameters unless required in the specification.
  • 3.6.A.2 When the return expression evaluates to an object reference, the reference is returned, not a reference to a new copy of the object.
  • 3.6.A.3 Methods cannot access the private data and methods of a parameter that holds a reference to an object unless the parameter is the same type as the method's enclosing class.
עברית

מטרות למידה 3.6.A: פיתוח קוד להגדרת התנהגויות של אובייקט באמצעות שיטות שנכתבו בכיתה, תוך שימוש ברפרנסים של אובייקטים, וקביעת התוצאה של קריאת שיטות אלו.

  • 3.6.A.1 כאשר הארגומנט הוא רפרנס של אובייקט, הפרמטר מצופיין בהעתק של הרפרנס הזה; הוא אינו יוצר העתק עצמאי חדש של האובייקט. אם הפרמטר מתייחס לאובייקט מתחלף (mutable), השיטה או הקונסטרוקטור יכולים להשתמש ברפרנס זה כדי לשנות את המצב של האובייקט. מנהג טוב בתכנות הוא לא לשנות אובייקטים מתחלפים שעברו כפרמטרים, אלא אם כן נדרש במפרט.
  • 3.6.A.2 כאשר ביטוי ההחזרה מוערך לרפרנס של אובייקט, הרפרנס מוחזר, ולא רפרנס להעתק חדש של האובייקט.
  • 3.6.A.3 שיטות אינן יכולות לגשת לנתונים פרטיים ולשיטות פרטיות של פרמטר המכיל רפרנס לאובייקט, אלא אם הפרמטר הוא מאותו סוג כמו הכיתה המקיפה את השיטה.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English
= copies the reference, not the object

When you pass an object to a method, Java copies the reference, so the method acts on the same object – changes to its fields are visible to the caller. (Primitives are copied by value, so changes to them are not.) A method can also return a reference to an object. Because a String is immutable, passing one is safe; passing a mutable object lets a method change it.

Exam skill: know that mutating an object's fields inside a method affects the original, but reassigning the parameter (param = new...) does not affect the caller.

Worked example. Suppose s is a Student with score 50, and we call tweak(s):

Line (1) changes the object s points to, so the caller now sees 100. Line (2) makes the method's own copy of the reference point at a fresh object – the caller's s is untouched – and line (3) affects only that new object. After the call, s.getScore() is 100: the mutation stuck, the reassignment did not.

עברית
= מעתיק את הייחוס, לא את האובייקט

כשעוברים אובייקט לשיטה, Java מעתיקה את הרפרנס, ולכן השיטה פועלת על האותו אובייקט – שינויים בשדותיו נראים לקורא. (פרימיטיבים מועברים בערך, ולכן שינויים בהם אינם נראים.) שיטה יכולה גם להשיב רפרנס לאובייקט. מכיוון ש-String הוא בלתי משתנה, העברתו בטוחה; העברת אובייקט משתנה מאפשרת לשיטה לשנות אותו.

Java מעבירה בערך: השיטה מקבלת העתק; העברה אמיתית לפי רפרנס, שאין ל-Java, הייתה מאפשרת לה להחליף את המשתנה של הקורא
Java תמיד מעבירה בערך (משמאל): השיטה מקבלת העתק של הרפרנס. העברה אמיתית לפי רפרנס (מימין) — שאין ל-Java — הייתה מאפשרת לשיטה להחליף את המשתנה עצמי של הקורא.

מיומנות למבחן: לדעת שהשמטת שדות של אובייקט בתוך שיטה משפיעה על המקורי, אך החלפת הפרמטר (param = new...) אינה משפיעה על הקורא.

דוגמה פתורה. נניח ש-s הוא Student עם ציון 50, וקוראים ל-tweak(s):

public static void tweak(Student a) {
    a.setScore(100);          // (1) mutates the shared object
    a = new Student("Z", 0);  // (2) repoints the local copy only
    a.setScore(5);            // (3) changes only the new local object
}

השורה (1) משנה את מה שהאובייקט s מצביע אליו, ולכן הקורא רואה כעת את 100. השורה (2) יוצרת העתק פרטית של הייחוס בתוך המטודה שמצביעה אל אובייקט חדש – הייחוס s של הקורא נותר ללא שינוי – והשורה (3) משפיעה רק על אובייקט חדש זה. לאחר השיחה, s.getScore() הוא 100: המוטציה התבצעה, אך ההחזת הייחוס לא.

3.7

Class Variables and Class Methods · ⁨משתני מחלקה ומשתני מחלקה (Static Methods)⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 3.7.A: Develop code to define behaviors of a class through class methods.

  • 3.7.A.1 Class methods cannot access or change the values of instance variables or call instance methods without being passed an instance of the class via a parameter.
  • 3.7.A.2 Class methods can access or change the values of class variables and can call other class methods.

Learning Objective 3.7.B: Develop code to declare the class variables that belong to the class.

  • 3.7.B.1 Class variables belong to the class, with all objects of a class sharing a single copy of the class variable. Class variables are designated with the static keyword before the variable type.
  • 3.7.B.2 Class variables that are designated public are accessed outside of the class by using the class name and the dot operator, since they are associated with a class, not objects of a class.
  • 3.7.B.3 When a variable is declared final, its value cannot be modified.
עברית

מטרות למידה 3.7.A: פיתוח קוד להגדרת התנהגויות של כיתה באמצעות שיטות כיתה.

  • 3.7.A.1 שיטות כיתה אינן יכולות לגשת או לשנות את ערכי משתני המוסף או לקרוא לשיטות מוסף ללא קבלת מוסף של הכיתה דרך פרמטר.
  • 3.7.A.2 שיטות כיתה יכולות לגשת או לשנות את ערכי משתני הכיתה ויכולות לקרוא לשיטות כיתה אחרות.

מטרות למידה 3.7.B: פיתוח קוד להצהרת משתני הכיתה השייכים לכיתה.

  • 3.7.B.1 משתני מחלקה שייכים למחלקה, וכל האובייקטים במחלקה משתפים עותק אחד של משתנה זה. משתני מחלקה מסומנים עם המילאה static לפני סוג המשתנה.
  • 3.7.B.2 משתני מחלקה שמסומנים public נגישים מחוץ למחלקה באמצעות שם המחלקה ואופרטור הנקודה, שכן הם קשורים למחלקה ולא לאובייקטים של המחלקה.
  • 3.7.B.3 כאשר משתנה מוגדר כ-final, הערך שלו אינו ניתן לשינוי.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English
static vs instance fields

A static (class) variable 类变量, marked static, is shared by all objects of the class – one copy total (e.g. a counter of how many objects exist). A static method belongs to the class and cannot use instance fields directly. Access them by class name: Student.getCount().

עברית
שדות static לעומת שדות instance

משתנה static (של מחלקה), המסומן static, משותף לכל האובייקטים של המחלקה – העתק אחד בסך הכל (למשל, סופר לכמה אובייקטים קיימים). שיטת static שייכת למחלקה ואינה יכולה להשתמש ישירות בשדות instance. גישה אליהם מתבצעת על ידי שם המחלקה: Student.getCount().

3.8

Scope and Access · ⁨תחום (Scope) וגישה⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 3.8.A: Explain where variables can be used in the code.

  • 3.8.A.1 Local variables are variables declared in the headers or bodies of blocks of code. Local variables can only be accessed in the block in which they are declared. Since constructors and methods are blocks of code, parameters to constructors or methods are also considered local variables. These variables may only be used within the constructor or method and cannot be declared to be public or private.
  • 3.8.A.2 When there is a local variable or parameter with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable within the body of the constructor or method.
עברית

מטרת הלמידה 3.8.A: הסבר היכן משתנים יכולים לשמש בקוד.

  • 3.8.A.1 משתנים מקומיים הם משתנים המוגדרים בתוך הכותרות או הגוף של בלוקי קוד. משתנים מקומיים ניתנים לגישה רק בתוך הבלוק שבו הם מוגדרים. מכיוון שבניינים ומתודות הם בלוקי קוד, הפרמטרים לבניינים ולמתודות נחשבים גם הם כמשתנים מקומיים. משתנים אלו יכולים לשמש רק בתוך הבונה או המתודה ואינם יכולים להיות מסומנים כ-public או כ-private.
  • 3.8.A.2 כאשר קיים משתנה מקומי או פרמטר בעל אותו שם כמושתנה מופע, השם יתייחס למשתנה המקומי במקום למשתנה המופע בתוך גוף הבונה או המתודה.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

Scope 作用域 is where a name is visible. A local variable declared in a method exists only inside it; a parameter exists only in its method; an instance variable is visible throughout the object. Access modifiers control visibility across classes: private (this class only) versus public (anywhere). Local variables shadow fields of the same name – a source of bugs.

עברית

תחום (Scope) הוא המקום שבו שם נתון נראה. משתנה מקומי שהוכרז בתוך שיטה קיים רק בתוכה; פרמטר קיים רק בשיטה שלו; משתנה instance נראה בכל האובייקט. מודפי גישה שולטים בנראות בין מחלקות: private (המחלקה הזו בלבד) מול public (בכל מקום). משתנים מקומיים מסתרים (Shadow) שדות באותו שם – זהו מקור ל bugs.

משתנה גלובלי נראה בכל מקום; משתנה לוקאלי רק בתוך הבלוק שלו
משתנה גלובלי נראה בכל מקום; משתנה מקומי רק בתוך הבלוק שלו
Vocabulary · ⁨מילון מונחים⁩ Train · ⁨אימון⁩
English עברית
Scope/skəʊp/ תחום תוקף
3.9

The this Keyword · ⁨המילה המפורשת this⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 3.9.A: Develop code for expressions that are self-referencing and determine the result of these expressions.

  • 3.9.A.1 Within an instance method or a constructor, the keyword this acts as a special variable that holds a reference to the current object—the object whose method or constructor is being called.
  • 3.9.A.2 The keyword this can be used to pass the current object as an argument in a method call.
  • 3.9.A.3 Class methods do not have a this reference.
עברית

מטרת הלמידה 3.9.A: פיתוח קוד עבור ביטויים המפנים לעצמם וקביעת התוצאה של ביטויים אלו.

  • 3.9.A.1 בתוך מתודת מופע או בונה, המילה this פועלת כמשתנה מיוחד המשמר הפניה לאובייקט הנוכחי—האובייקט שמתודתו או בונהו נקראים.
  • 3.9.A.2 המילה this יכולה לשמש להעברת האובייקט הנוכחי כארגומנט בקריאת מתודה.
  • 3.9.A.3 מתודות מחלקה אינן מכילות הפניה ל-this.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

this is a reference to the current object. Use it to tell a field apart from a parameter with the same name, or to call another method of the same object:

Exam skill: when a constructor or setter's parameter has the same name as a field, you must write this.field = param – without this, the assignment does nothing useful.

עברית

this הוא רפרנס לאובייקט הנוכחי. ניתן להשתמש בו כדי להבדיל בין שדה לבין פרמטר באותו שם, או לקרוא לשיטה אחרת באותו אובייקט:

public Student(String name, int score) {
    this.name = name;      // this.name is the field; name is the parameter
    this.score = score;
}

מיומנות למבחן: כאשר לפרמטר של Constructor או Setter יש אותו שם כמו לשדה, חובה לכתוב this.field = param – ללא this, ההקלדה אינה עושה דבר שימושי.

3.9

Exam tips · ⁨טיפים לבחינות⁩

English
  • Design with methods and classes: encapsulate data as private fields and expose behaviour through public methods.
  • Know the difference between an object and its class, and that objects are passed by value — the parameter gets a copy of the reference, so a method can change the object's state, but reassigning the parameter does not affect the caller (Java has no pass-by-reference).
  • Traverse arrays and ArrayLists safely — size is length vs .size(), and removing during a loop shifts indices.
  • Trace a recursive method to determine its result: find the base case first, then follow each recursive call to its return value (writing recursive code is outside the exam's scope).
  • Recognise inheritance vocabulary — superclass, subclass, method overriding, and that every class is a subclass of Object (designing and implementing inheritance is outside the exam's scope).
עברית
  • עיצוב עם שיטות וכיתות: אקפסולציה נתונים כשדות private וחשיפת התנהגות דרך שיטות ציבוריות.
  • להבין את ההבדל בין אובייקט לכיתה שלו, ולעובדה שאובייקטים עוברים ערך — הפרמטר מקבל העתק של הייחוס, כך ששיטה יכולה לשנות את מצב האובייקט, אך השבתת התייחסות בפרמטר אינה משפיעה על הקורא (ב-Java אין מעבר בהפניה).
  • לעבור על מערכים ועל ArrayList בטוח — הגודל הוא length מול .size(), והסרה במהלך לולאה מזיזה את האינדקסים.
  • מעקב בשיטה רקורסיבית כדי לקבוע תוצאתה: למצוא תחילה את המקרה הבסיסי, ולאחר מכן לעקוב אחרי כל קריאה רקורסיבית עד לתוצאת החזרה (כתיבת קוד רקורסיבי היא מחוץ להיקף המבחן).
  • לזהות מושגי ירישה: כיתה עליונה, כיתה תחתונה, חפיסת שיטה, וכל כיתה היא כיתה תחתונה של Object (עיצוב והטמעת ירישה הם מחוץ להיקף המבחן).

Interactive lessons on this topic · ⁨שיעורים אינטראקטיביים בנושא זה⁩

Work through it step by step, with instant-check exercises. · ⁨לעבור על הדברים צעד אחר צעד, עם תרגילים לבדיקה מיידית.⁩

Past Papers · ⁨מבחני עבר⁩

More topics in AP Computer Science A · ⁨מדעי מחשב A - AP⁩ · ⁨נושאים נוספים בAP Computer Science A · ⁨מדעי מחשב A - AP⁩⁩

Log in or create account · ⁨היכנס או צור חשבון⁩

IGCSE, A-Level & AP