أنماط السجلات (Record patterns)
في هذا الدرس 4 موضوعات
محتوى هذا الدرس مكتوب وينتظر المراجعة. يعرض المخطط أدناه ما سيغطيه، وسيُنشر بعد اعتماده.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
take a record apart directly in a pattern.
الموضوعات المخطط لها
- A record pattern pulls out the components:
case Percentage(int percent). - It works in
switchand ininstanceof. - Patterns can be nested, for example a record inside a record.
- Use it when you only need the parts, not the whole object.
- Stop nesting when the line becomes hard to read.
تفاصيل المخطط المبدئي
عرض عملي
return switch (d) {
case Percentage(int percent) -> price.minusPercent(percent);
case FixedAmount(Money amount) -> price.minus(amount);
case BuyXGetY(int buy, int free) -> price;
};اختبر فهمك
What is the benefit of case Percentage(int percent) over case Percentage p?
أظهر الإجابة
You get the value directly, without calling p.percent().
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.