السجلات (Records)
في هذا الدرس 4 موضوعات
محتوى هذا الدرس مكتوب وينتظر المراجعة. يعرض المخطط أدناه ما سيغطيه، وسيُنشر بعد اعتماده.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
replace a boilerplate data class with a record.
الموضوعات المخطط لها
- A record is a short way to write an immutable data class.
- Java generates the constructor, getters (like
name()),equals,hashCode, andtoString. - A compact constructor has no parameter list. Use it to validate the values.
- Records are shallow: if a component is a mutable list, copy it with
List.copyOf. - Records cannot have setters or extra instance fields.
تفاصيل المخطط المبدئي
عرض عملي
public record Money(long cents, String currency) {
public Money {
if (cents < 0) throw new IllegalArgumentException("negative");
currency = currency.toUpperCase();
}
}اختبر فهمك
What does a compact constructor do?
أظهر الإجابة
It runs checks or cleans values before the fields are set.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.