Skip to content · ⁨الانتقال إلى المحتوى⁩

Class Creation · ⁨إنشاء الفئة⁩

AP Computer Science A · ⁨AP علوم الحاسوب A⁩ · 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 يوفر التجريد الإجرائي اسمًا لعملية ويتيح استخدام الطريقة بمعرفة ما تفعله فقط، وليس كيفيةقيام بها. من خلال تحليل الطرق، يقوم المبرمج بتقسيم السلوكيات الكبيرة للفئة إلى سلوكيات أصغر من خلال إنشاء طرق لتمثيل كل سلوك فردي أصغر. قد يستخرج التجريد الإجرائي ميزات مشتركة لتعميل الوظيفة بدلاً من تكرار الكود. هذا يسمح بإعادة استخدام الكود، مما يساعد في إدارة التعقيد.
  • 3.1.A.5 استخدام المعاملات يسمح بتعميل الإجراءات، مما يتيح إعادة استخدامها مع مجموعة من قيم الإدخال أو المعاملات.
  • 3.1.A.6 استخدام التجريد الإجرائي في البرنامج يسمح للمبرمجين بتغيير internals الخاص بالطريقة (لجعلها أسرع، أكثر كفاءة، استخدام تخزين أقل، إلخ.) دون الحاجة إلى إبلاغ مستخدمي الطريقة بالتغيير طالما أن توقيع الطريقة وما تفعله الطريقة محفوظ.
  • 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.

العربية

تؤثر خيارات التصميم على ما إذا كان الكود صحيحًا، ومقروءًا، وقابل لإعادة الاستخدام. التغليف – إبقاء البيانات خاصة وكشفها فقط عبر الدوال – يحمي حالة الكائن من سوء الاستخدام ويسمح لك بتغيير internals دون كسر عمل مستخدمي الفئة. التسمية المدروسة، والدوال ذات الغرض الواحد، والاختبار تقلل الأخطاء.

يتضمن التصميم أيضًا مسؤولية تتجاوز الكود. موثوقية النظام - أداء برنامج لمهامه كما هو متوقع، بدون فشل - هو ما يجب أن يسعى المبرمجون لتعظيمه من خلال التصميم الدقيق والاختبار. للبرامج تأثيرات حقيقية على المجتمع والاقتصاد والثقافة يمكن أن تكون مفيدة وضارة معًا. وإنتاج البرامج يثير قضايا قانونية وحقوق الملكية الفكرية: غالبًا ما يعيد المبرمجون استخدام الكود المنشور كمصدر مفتوح ومجاني الاستخدام، لكنهم يجب أن يحترموا ترخيصه ويمنحوه التقدير بدلاً من نسخ عمل الآخرين باعتباره عملهم الخاص.

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 المتغيرات العينية تنتمي إلى الكائن، ولكل كائن نسخة خاصة به من المتغير.
  • 3.3.A.5 يجب إبقاء الوصول إلى السمات داخلياً داخل الفئة لتحقيق التغليف. لذلك، فإن ممارسة البرمجة الجيدة هي تحديد متغيرات الكائن الخاصة بهذه السمات بـ 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:

العربية

الفئة لها ثلاثة أجزاء: متغيرات实例 (حقول – بيانات الكائن)، ومُنشئات (بناء الكائنات)، ودوال (سلوك). الحقول عادةً ما تكون 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 · ⁨رshan حقول كائن كصناديق⁩

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/ المتغيرات العينية
constructor/kənˈstrʌktə/ الدالة الإنشائية
overloading/ˌəʊvəˈləʊdɪŋ/ إعادة التحميل
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 يشير حالة الكائن إلى سماته وقيمتها عند وقت معين، وهي معرفة بواسطة المتغيرات العينية التابعة للكائن. وهذا يحدد علاقة "يحتوي على" بين الكائن ومتغيراته العينية.
  • 3.4.A.2 يُستخدم المُشَكِّل لتحديد الحالة الأولية للكائن، والتي يجب أن تشمل قيمًا أولية لجميع المتغيرات العينية. عند استدعاء المُشَكِّل، يتم تخصيص الذاكرة للكائن وإرجاع مرجع الكائن المرتبط به. توفر معاملات المُشَكِّل، إن تم تحديدها، بيانات لتهيئة المتغيرات العينية.
  • 3.4.A.3 عندما يكون الكائن القابل للتعديل هو معلمة مُشَكِّل، يجب تهيئة المتغير العيني بنسخة من الكائن المرجعي. وبهذه الطريقة، لا يحمل المتغير العيني مرجعاً للكائن الأصلي، ويتم منع الدوال من تعديل حالة الكائن الأصلي.
  • 3.4.A.4 عند عدم كتابة مُشَكِّل، يوفر Java مُشَكِّل بدون معلمات، وتُضبط المتغيرات العينية على القيم الافتراضية وفقاً لنوع بيانات السمة. يُسمى هذا المُشَكِّل "المُشَكِّل الافتراضي".
  • 3.4.A.5 القيمة الافتراضية لسمة من نوع int هي 0. القيمة الافتراضية لسمة من نوع double هي 0.0. القيمة الافتراضية لسمة من نوع boolean هي false. القيمة الافتراضية لنوع مرجعي هي 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.

العربية

الـمنشئ له نفس اسم الفئة ولا نوع إرجاع. يتم تنفيذه عند كتابة new، ومهمته تهيئة الحقول. يمكن للفئة أن تحتوي على عدة منشئات بقوائم معاملات مختلفة (إعادة التحميل)؛ المنشئ بدون معاملات يحدد قيم افتراضية.

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.
  • 3.5.A.3 في الدوال غير الفارغة، يتم تقييم تعبير إرجاع متوافق مع نوع الإرجاع، وتُرجع القيمة. يُشار إلى هذا باسم "الإرجاع بالقيمة".
  • 3.5.A.4 تُستخدم الكلمة return لإرجاع تدفق التحكم إلى النقطة التي تم فيها استدعاء الدالة أو المُشَكِّل. لن يتم تنفيذ أي كود يأتي تسلسلياً بعد جملة الإرجاع أبداً. تنفيذ جملة إرجاع داخل جملة اختيار أو تكرار سيوقف الجملة ويخرج من الدالة أو المُشَكِّل.
  • 3.5.A.5 دالة الحاصِل تسمح لكائنات الفئات الأخرى بالحصول على نسخة من قيمة المتغيرات العينية أو متغيرات الفئة. دالة الحاصل هي دالة غير فارغة.
  • 3.5.A.6 دالة المُعدِّل (المغيِّر) هي دالة تقوم بتغيير قيم المتغيرات العينية أو متغيرات الفئة. غالباً ما تكون دالة المُعدِّل دالة فارغة.
  • 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ə/ دالة مُعدِّلة (setter)
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 عندما يكون الحجة مرجعًا إلى كائن، يتم تهيئة المعامل بنسخة من ذلك المرجع؛ ولا ينشئ نسخة مستقلة جديدة للكائن. إذا كان المعامل يشير إلى كائن قابل للتعديل، يمكن للطريقة أو المُنشئ استخدام هذا المرجع لتغيير حالة الكائن. يُعدّ عدم تعديل الكائنات القابلة للتعديل التي تُمرَر كمعاملات ممارسة برمجية جيدة ما لم يكن مطلوبًا في المواصفات.
  • 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 · ⁨متغيرات الفئة ودوال الفئة⁩

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().

العربية
الحقول الساكنة مقابل متغيرات实例

متغير instance (فئة) ساكن، مُشار إليه بـ static، يتم مشاركته بواسطة جميع كائنات الفئة – نسخة واحدة إجماليًا (مثل عداد لعدد الكائنات الموجودة). الدالة الساكنة تابعة للفئة ولا يمكنها استخدام متغيرات instance مباشرة. الوصول إليها باسم الفئة: Student.getCount().

3.8

Scope and Access · ⁨النطاق والوصول⁩

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.

العربية

النطاق هو المكان الذي يظهر فيه الاسم. متغير local مُعلن في دالة يوجد فقط بداخلها؛ معامل يوجد فقط في دالته؛ متغير instance مرئي طوال الكائن. تحكم تعديلات الوصول في الظهور عبر الفئات: private (هذه الفئة فقط) مقابل public (في أي مكان). متغيرات local تظلل الحقول بنفس الاسم – مصدر للأخطاء.

متغير global مرئي في كل مكان؛ متغير local فقط داخل كتلته
متغير global مرئي في كل مكان؛ متغير local فقط داخل كتلته
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 لتمرير الكائن الحالي作为 argument في استدعاء طريقة.
  • 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;
}

مهارة الامتحان: عندما تكون معلمة المُنشئ أو المُعدِّل تحمل نفس اسم الحقل، يجب عليك كتابة 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 لا تدعم التمرير بالمرجح).
  • مرر عبر المصفوفات وArrayLists بأمان — الحجم هو length مقابل .size()، وإزالة عنصر أثناء حلقة يزيح الفهارس.
  • تتبع الطريقة العودية لتحديد نتيجتها: ابحث أولاً عن الحالة الأساسية، ثم اتبع كل استدعاء عودي حتى قيمته الإرجاعية (كتابة الكود العودي خارج نطاق الامتحان).
  • اعرف مصطلحات الإرث — الفئة الأب، الفئة الابن، استبدال الطريقة، وأن كل فئة هي فئة ابن لـ Object (تصميم وتنفيذ الإرث خارج نطاق الامتحان).

Interactive lessons on this topic · ⁨دروس تفاعلية حول هذا الموضوع⁩

Work through it step by step, with instant-check exercises. · ⁨ا-working عليه خطوة بخطوة، مع تمارين تحقق فوري.⁩

Past Papers · ⁨أوراق الامتحانات السابقة⁩

More topics in AP Computer Science A · ⁨AP علوم الحاسوب A⁩ · ⁨المزيد من المواضيع في AP Computer Science A · ⁨AP علوم الحاسوب A⁩⁩

Log in or create account · ⁨تسجيل الدخول أو إنشاء حساب⁩

IGCSE, A-Level & AP