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

الأنواع العامة (Generics)

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

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

نظرة عامة

الموضوعات

  • الفئات العامة
  • الدوال
  • معاملات النوع (type parameters)
  • أساسيات wildcards

تعلّم أولًا

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

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

  1. Explain why generics exist (type safety without casts).
  2. Write a generic class (Box<T>, Pair<A, B>) and a generic method.
  3. Read signatures with bounded types and wildcards (<T extends Comparable<T>>, List<? extends Number>).

الدروس

  1. 01الحياة قبل Generics10 د قراءة
  2. 02الفئات العامة9 د قراءة
  3. 03الدوال العامة10 د قراءة
  4. 04الأنواع المقيّدة (Bounded types)10 د قراءة
  5. 05Wildcards: قراءة توقيعات JDK11 د قراءة
  6. 06عرض عملي: بناء مستودع عام داخل الذاكرة10 د قراءة

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

Lab

BEG-031-LAB1 · Generic repository

Outcomes: 1–3 · Time: ~2 h Write InMemoryRepository<T, ID> with save, findById, findAll, delete. Use it for two different types (for example Contact and Event). Show one line that fails to compile when the wrong type is saved. Acceptance: no casts; compile-time type safety demonstrated.

Exercises (assessment: four exercises)

IDOutcomeTask
BEG-031-EX011Explain in two sentences what goes wrong with a raw List holding mixed types, and how List<String> fixes it.
BEG-031-EX022Write Pair<A, B> with a constructor and getters; create Pair<String, Integer>.
BEG-031-EX032, 3Write static <T extends Comparable<T>> T max(List<T> list).
BEG-031-EX043Write static double sum(List<? extends Number> nums) and call it with List<Integer> and List<Double>.
أظهر الحلول
  • EX01 A raw list accepts anything, so a bad element is found only when a cast fails at runtime. List<String> makes the compiler reject non-strings and removes the cast.
  • EX02 class Pair<A,B>{ private final A first; private final B second; Pair(A a,B b){first=a;second=b;} A getFirst(){return first;} B getSecond(){return second;} }
  • EX03 Loop keeping best; replace it when item.compareTo(best) > 0. Return best.
  • EX04 double total = 0; for (Number n : nums) total += n.doubleValue(); return total;
  • The common mistakes (raw types, new T()) are covered in demo L06; the lab covers outcomes 1–2 as well.

الاختبار

Outcomes: O1 why generics · O2 generic classes and methods · O3 bounds and wildcards.

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

  1. 1Generics mainly give you:O1
  2. 2Without generics, a wrong cast fails:O1
  3. 3List<String> l = ...; l.add(5); results in:O1
  4. 4In class Box<T>, T is:O2
  5. 5Which is valid?O2
  6. 6In a generic method, <T> goes:O2
  7. 7<T extends Comparable<T>> lets you call:O3
  8. 8List<? extends Number> is best described as:O3
  9. 9Which line does not compile for a type parameter T?O2
  10. 10Is List<Integer> a List<Number>?O3

الجزء العملي

Four exercises BEG-031-EX01 to EX04 plus the lab. Pass: all four exercises correct and the repository compiles without casts or raw types.

أخطاء شائعة

Raw types; trying new T().

التقييم

4 exercises.

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

Spring Data's JpaRepository<T, ID> (JNR-029) is exactly this pattern.

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