تشريح الكلاس
مُقدَّم| English | المعنى |
|---|---|
| encapsulation/ɪnˌkæpsjʊˈleɪʃn/ | التغليف |
| instance variables/ˈɪnstəns ˈveərɪəblz/ | متغيرات العينة |
Private fields still need checked methods
- A Student has a private score, but
public void setScore(int s) { score = s; }still lets a caller supply -5. Private access alone does not enforce a non-negative score. - A guarded setter can reject that update. Check the constructor and every other method that writes the field too; encapsulation makes a validation policy easier to enforce but does not create it automatically.
جعل الدرجة خاصة (private) يرفض تلقائيًا القيم السالبة المُقدمة لمُعرِّف عام (public setter) غير محمي.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
خطأ: الكود الخاص بالمُعرِّف ينتمي إلى الفئة ويمكنه تعيين القيمة السالبة. يجب تطبيق التحقق من صحة البيانات.
The three parts of a class
- The class designs here contain instance fields for data, constructors for initial setup, and methods for behavior. These are three useful parts to identify; formally, Java does not classify constructors as class members.
- Trace a Student from construction to an update: the constructor initializes its score field, and an instance method later reads or changes that object's score. A static method instead has no implicit current object.

A class diagram: private attributes and public methods
Instance variables hold state
- Instance variables 实例变量 store each object's own state.
private String name; private int score;— declared inside the class, outside any method.- Every object gets its own copy — one student's
scoreis separate from another's. - These persist for the life of the object (unlike local variables).
private and public
- Access modifiers control who can reach a member.
private— usable only inside the class;public— exposed to other code that can access the class.- Instance variables are usually
private; methods that form the interface arepublic. - This hiding of internal data is called encapsulation 封装.
ما الأجزاء الثلاثة التي تنظم تصميم فئة Student الموضحة هنا؟
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
تحفظ الحقول العينية حالة الكائن، وتقوم الدوال الإنشائية (Constructors) بتهيئة كائنات جديدة، بينما توفر الدوال (Methods) السلوك. لا تُعدّ الدوال الإنشائية أعضاءً رسميةً للفئة في مصطلحات لغة Java.
متغيرات العينة عادة ما يتم الإعلان عنها...
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
البيانات الخاصة + الدوال العامة = التغليف.
حفظ البيانات خاصة وكشفها فقط عبر الدوال يسمى ___ (كلمة واحدة).
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
التغليف يحمي بيانات الكائن.
كائنان من نفس الكلاس يشاركان نسخة واحدة من كل متغير عينة.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
لكل كائن نسخه الخاصة من متغيرات العينة.
بالنسبة لفئة Student العلوية الموضحة هنا، قد يقرأ فئة خارجية غير مرتبطة مباشرة حقلًا خاصًا...
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
يمنع الوصول الخاص فئة خارجية غير مرتبطة من قراءة الحقل مباشرة. يمكن للكود الموجود داخل Student قراءة حقل خاص لكائن Student آخر.
Why keep data private
- Encapsulation prevents unrelated external code from directly assigning a private field. Code inside Student can still read or write private fields, including those of another explicitly referenced Student.
- Outsiders interact only through public methods, which can validate and control access.
- You could reject a negative score in a method; a public variable has no such guard.
- Private fields with a deliberately chosen public interface support controlled updates. Each method and constructor must still implement the intended rules; returning a mutable internal object can also expose changes indirectly.
Keep instance variables private; expose behavior through public methods. A public field lets any code set it to anything (a negative score, a null name) with no checks. Encapsulation funnels all access through methods that can validate the change. Each object has separate instance-field storage, but reference fields in different objects may point to the same shared object. Distinguish separate fields from separate referenced objects.
لديك كائنان بحقول مرجعية منفصلة، لذا يجب أن تشير تلك الحقول إلى كائنات مختلفة.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
خطأ: يمكن لحقول مرجعية منفصلة أن تشير جميعها إلى نفس الكائن القابل للتعديل (mutable object).
يرجع طالبان إلى نفس مصفوفةArrayList فارغة ابتدائيًا. يضيف أحدهما عنصرًا واحدًا. ما حجم القائمة الذي يلاحظه المرجع الآخر؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
يشير كلا المرجعين إلى نفس كائن القائمة، whose size is now 1.
Anatomy of a Student class:
- Instance variables:
private String name; private int score; - Constructor:
public Student(String n) { ... } - Methods:
public void addScore(int s) { ... },public int getScore() { ... }
Separate field storage from shared objects
- If two Student objects both store a reference to the same ArrayList, their list fields are separate but the referenced list is shared. Adding an item through either reference changes that one list.
- Reassigning one student's list field to a new ArrayList changes only that field. Follow reference arrows when predicting state; private access and object identity answer different questions.
A class has instance variables (each object's state), constructors (set up new objects), and methods (behaviors). Instance variables are usually private and accessed through public methods — this encapsulation protects the data. Each object holds separate instance fields; the objects referenced by those fields may still be shared.
لكل كائن حالته الخاصة
a.score و b.score منفصلان — كل كائن يملك حالته.