الفئات والواجهات المختومة (Sealed)
في هذا الدرس 4 موضوعات
محتوى هذا الدرس مكتوب وينتظر المراجعة. يعرض المخطط أدناه ما سيغطيه، وسيُنشر بعد اعتماده.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
model a closed set of types.
الموضوعات المخطط لها
sealedlists exactly which classes may implement an interface.- Like a crate label: no other kind of crate can exist.
- Use
permitsto name the allowed types. - Each allowed type must be
final,sealed, ornon-sealed. Records are final already. - The compiler now knows every case, which helps
switch.
تفاصيل المخطط المبدئي
عرض عملي
sealed interface Discount permits Percentage, FixedAmount, BuyXGetY {}
record Percentage(int percent) implements Discount {}
record FixedAmount(Money amount) implements Discount {}
record BuyXGetY(int buy, int free) implements Discount {}اختبر فهمك
What does permits do?
أظهر الإجابة
It names the only types allowed to implement the sealed interface.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.