الأنواع العامة (Generics)
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- الفئات العامة
- الدوال
- معاملات النوع (type parameters)
- أساسيات wildcards
تعلّم أولًا
60 دقيقة قراءة · 3 ساعة تدريب (تقدير)
مخرجات التعلم
- Explain why generics exist (type safety without casts).
- Write a generic class (
Box<T>,Pair<A, B>) and a generic method. - Read signatures with bounded types and wildcards (
<T extends Comparable<T>>,List<? extends Number>).
الدروس
المختبر والتمارين
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)
| ID | Outcome | Task |
|---|---|---|
| BEG-031-EX01 | 1 | Explain in two sentences what goes wrong with a raw List holding mixed types, and how List<String> fixes it. |
| BEG-031-EX02 | 2 | Write Pair<A, B> with a constructor and getters; create Pair<String, Integer>. |
| BEG-031-EX03 | 2, 3 | Write static <T extends Comparable<T>> T max(List<T> list). |
| BEG-031-EX04 | 3 | Write 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 whenitem.compareTo(best) > 0. Returnbest. - 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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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.