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

نموذج الكائنات في Java

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

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

نظرة عامة

الموضوعات

  • Object
  • equals()
  • hashCode()
  • toString()
  • الهوية مقابل المساواة

تعلّم أولًا

60 دقيقة قراءة · 3 ساعة تدريب (تقدير)

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

  1. Explain that every class extends Object and name its key methods.
  2. Distinguish identity (==) from equality (equals) and fix string comparison bugs.
  3. Implement equals and hashCode consistently and explain the contract.
  4. Override toString for useful logging and debugging.

الدروس

  1. 01الفئة Object10 د قراءة
  2. 02الهوية مقابل المساواة10 د قراءة
  3. 03عقد equals/hashCode10 د قراءة
  4. 04toString10 د قراءة
  5. 05السجلات (Records) كمعاينة: مساواة مجانية10 د قراءة
  6. 06عرض عملي: HashSet المعطّل وإصلاح equals/hashCode10 د قراءة

المختبر والتمارين

Lab

BEG-026-LAB1 · Broken set

Outcomes: 2, 3, 4 · Time: ~1.5 h Put Money objects into a HashSet (treat it as a black box) with no overrides, with only equals, and with both equals and hashCode. Record the set size each time and explain the duplicates. Acceptance: correct implementation and explanation.

Exercises (assessment: contract quiz plus implementation exercise)

IDOutcomeTask
BEG-026-EX011List five methods every object inherits from Object, and say which three are most often overridden.
BEG-026-EX022Predict == and equals for: two literals "a", new String("a") vs a literal, two new Account(...) with the same data and no overrides.
BEG-026-EX032Fix: if (command == "quit").
BEG-026-EX043Implement equals and hashCode for Customer(id, name) where id identifies the customer.
BEG-026-EX053Explain why a mutable field in hashCode can make an object "disappear" from a HashSet.
BEG-026-EX064Write a toString for Account that shows number and balance but not secrets.
أظهر الحلول
  • EX01 toString, equals, hashCode, getClass, wait/notify (or clone). Most overridden: toString, equals, hashCode.
  • EX02 "a" == "a" true (shared literal), equals true; new String("a") == "a" false, equals true; two accounts: == false, equals false.
  • EX03 "quit".equals(command) (also safe against null).
  • EX04 Compare id only: equals checks o instanceof Customer c && id == c.id; hashCode returns Long.hashCode(id) or Objects.hash(id).
  • EX05 The object was stored under a bucket from its old hash; after the field changes, lookups use the new hash and look in another bucket.
  • EX06 return "Account[" + number + ", balance=" + balance + "]";
  • Outcome 3 is also covered by the lab and the quiz.

الاختبار

Outcomes: O1 Object · O2 identity vs equality · O3 equals/hashCode · O4 toString.

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

  1. 1Every Java class extends, directly or indirectly:O1
  2. 2Which method does Object NOT have?O1
  3. 3== on two object references checks:O2
  4. 4The correct way to compare two strings' text:O2
  5. 5The default equals from Object behaves like:O2
  6. 6If a.equals(b) is true, then:O3
  7. 7Overriding equals but not hashCode can cause:O3
  8. 8Which field is risky to use in hashCode?O3
  9. 9toString output should NOT contain:O4
  10. 10A record automatically gets:O3

الجزء العملي

Contract quiz above plus implementation exercise BEG-026-EX04 and the broken-set lab. Pass: quiz 8/10 and a correct equals/hashCode pair with the duplicate behavior explained.

أخطاء شائعة

Overriding equals without hashCode; using mutable fields in hashCode.

التقييم

Contract quiz plus implementation exercise.

الصلة بالمسار الوظيفي

Essential for collections (BEG-030), hashing (BEG-041), and JPA entities (JNR-028).

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