انتقل إلى المحتوى
أمين أبوزيد
BEG-020

الفئات والكائنات

  • 7 درس
  • 80 دقيقة قراءة
  • 4 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • الحقول (Fields)
  • الدوال البانية (constructors)
  • الدوال
  • this
  • إنشاء الكائنات

تعلّم أولًا

80 دقيقة قراءة · 4 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Write a class with fields, constructors (including overloaded ones), and instance methods.
  2. Create objects with new and call their methods.
  3. Use this to refer to fields and to chain constructors.
  4. Explain the difference between instance and static members and choose correctly.

الدروس

  1. 01كتابة أول فئة لك12 د قراءة
  2. 02الدوال البانية (Constructors)11 د قراءة
  3. 03دوال النسخة وthis11 د قراءة
  4. 04إنشاء الكائنات واستخدامها11 د قراءة
  5. 05static: الانتماء إلى الفئة12 د قراءة
  6. 06عرض عملي: الحسابات المصرفية v1 من ملف فارغ11 د قراءة
  7. 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)

IDOutcomeTask
BEG-020-EX011Write a Book class with title, author, pages, and two constructors (one with pages defaulting to 100).
BEG-020-EX022Create two Book objects, change one's pages, and show the other is unchanged.
BEG-020-EX033Find and fix the bug: Book(String title) { title = title; }.
BEG-020-EX043Rewrite the second constructor of EX01 to use this(...) to call the first.
BEG-020-EX054Decide 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; then b.pages still prints its own value; each object has its own copy.
  • EX03 The parameter is assigned to itself and the field stays null. Use this.title = title;.
  • EX04 Shown in EX01: this(title, author, 100); must be the first statement.
  • EX05 pages instance; Book.count static (shared); isLong() instance (uses this book's pages); MAX_PAGES static 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.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1A constructor:O1
  2. 2If you write any constructor yourself, the free no-argument one:O1
  3. 3Several constructors with different parameters is called:O1
  4. 4The default value of an int field is:O1
  5. 5To create an object:O2
  6. 6After Account b = a; there are:O2
  7. 7Calling a method on a null reference throws:O2
  8. 8In Account(String owner) { owner = owner; } the field:O3
  9. 9this(...) inside a constructor:O3
  10. 10Which should be static?O4

الجزء العملي

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.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.