الفئات والكائنات
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- الحقول (Fields)
- الدوال البانية (constructors)
- الدوال
- this
- إنشاء الكائنات
تعلّم أولًا
80 دقيقة قراءة · 4 ساعة تدريب (تقدير)
مخرجات التعلم
- Write a class with fields, constructors (including overloaded ones), and instance methods.
- Create objects with
newand call their methods. - Use
thisto refer to fields and to chain constructors. - Explain the difference between instance and
staticmembers and choose correctly.
الدروس
- 01كتابة أول فئة لك12 د قراءة
- 02الدوال البانية (Constructors)11 د قراءة
- 03دوال النسخة وthis11 د قراءة
- 04إنشاء الكائنات واستخدامها11 د قراءة
- 05static: الانتماء إلى الفئة12 د قراءة
- 06عرض عملي: الحسابات المصرفية v1 من ملف فارغ11 د قراءة
- 07عرض عملي: static مقابل النسخة، كما تُرى في المصحّح (debugger)12 د قراءة
المختبر والتمارين
Lab
BEG-020-LAB1 · Bank Accounts v1
Outcomes: 1, 2, 3, 4 · Time: ~2 h Write an Account class with owner, number, and balance; a full constructor and an overloaded one; deposit and withdraw. In main, create several accounts and use them. Add a static counter of accounts created. Acceptance: compiles, works, no logic in main beyond wiring.
Exercises (assessment: 5 exercises plus Bank Accounts v1)
| ID | Outcome | Task |
|---|---|---|
| BEG-020-EX01 | 1 | Write a Book class with title, author, pages, and two constructors (one with pages defaulting to 100). |
| BEG-020-EX02 | 2 | Create two Book objects, change one's pages, and show the other is unchanged. |
| BEG-020-EX03 | 3 | Find and fix the bug: Book(String title) { title = title; }. |
| BEG-020-EX04 | 3 | Rewrite the second constructor of EX01 to use this(...) to call the first. |
| BEG-020-EX05 | 4 | Decide static or instance for: pages, Book.count, isLong(), MAX_PAGES. Justify each. |
أظهر الحلول
- EX01
java public class Book { String title, author; int pages; Book(String title, String author, int pages) { this.title = title; this.author = author; this.pages = pages; } Book(String title, String author) { this(title, author, 100); } } - EX02
a.pages = 300;thenb.pagesstill prints its own value; each object has its own copy. - EX03 The parameter is assigned to itself and the field stays
null. Usethis.title = title;. - EX04 Shown in EX01:
this(title, author, 100);must be the first statement. - EX05
pagesinstance;Book.countstatic (shared);isLong()instance (uses this book's pages);MAX_PAGESstatic final constant. - Outcomes 1-4 are also covered by the lab and quiz.
الاختبار
Outcomes: O1 write a class · O2 create objects · O3 this · O4 static vs instance.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Five exercises BEG-020-EX01 to EX05 plus Bank Accounts v1. Pass: at least four exercises correct and the lab meeting its acceptance.
أخطاء شائعة
Everything static; constructors that do heavy work; shadowing fields without this.
التقييم
5 exercises plus Bank Accounts v1.
الصلة بالمسار الوظيفي
Every Spring bean (JNR-009) and entity (JNR-028) is a class.