استبدال ليسكوف (Liskov Substitution)
في هذا الدرس 4 موضوعات
محتوى هذا الدرس مكتوب وينتظر المراجعة. يعرض المخطط أدناه ما سيغطيه، وسيُنشر بعد اعتماده.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
make subtypes safe to use in place of their parent.
الموضوعات المخطط لها
- L: code that uses the parent type must work with any subtype.
- Like the socket: any plug that fits must work.
- Violation: a subclass throws
UnsupportedOperationExceptionfor an inherited method. - Violation: a subclass breaks a promise of the parent, such as "never returns null".
- Fix: change the hierarchy or use composition.
تفاصيل المخطط المبدئي
عرض عملي
class Penguin extends Bird { void fly() { throw new UnsupportedOperationException(); } }
// callers of Bird.fly() breakاختبر فهمك
Why is throwing UnsupportedOperationException in an override a smell?
أظهر الإجابة
Callers of the parent type break.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.