RASPBERRY_PYTHON Telegram 6502
Liskov Substitution Principle:

خانوم Barbara Liskov در سال ۱۹۸۷ یه اصلی رو به نام LSP معرفی کردن که یکی از اصول طراحی کلاس ها در object oriented programming هست.

به طور خلاصه این اصل میگه اگر تایپ B از تایپ A ارث بری میکنه، هرجایی که ما نمونه ای از تایپ A داشته باشیم باید بتونیم به جاش نمونه ای از تایپ B رو قرار بدیم بدون اینکه برنامه به مشکل بخوره. در غیر این صورت این رابطه ارث بری صحیح نیست.

مثال: فرض کنید یه کلاس base داریم به اسم Bird که یک متد داره به اسم Fly:
class Bird:
def fly(self):
print("I can fly...")
و دو تا subclass داریم به اسم Duck (اردک) و Ostrich (شترمرغ) که از Bird ارث بری میکنن:
class Duck(Bird):
pass

class Ostrich(Bird):
pass
الان اردک میتونه پرواز کنه چون یه پرنده هست، ولی آیا شترمرغ میتونه پرواز کنه؟ نه نمیتونه. آیا پرنده نیست؟ چرا هست. پس این رابطه درست نیست و اصل LSP رو رعایت نمیکنه.

رابطه ی صحیح چطور میتونه باشه؟ اینکه تشخیص بدیم اصلا Fly متد درستی برای Bird نبوده و اون رو این شکلی بازنویسی کنیم:
class Bird:
pass

class FlyingBirds(Bird):
def fly(self):
print("I can fly...")

class Duck(FlyingBirds):
pass

class Ostrich(Bird):
pass

✒️ @Amirsoroushh
👍17🤔4🔥1



tgoop.com/raspberry_python/6502
Create:
Last Update:

Liskov Substitution Principle:

خانوم Barbara Liskov در سال ۱۹۸۷ یه اصلی رو به نام LSP معرفی کردن که یکی از اصول طراحی کلاس ها در object oriented programming هست.

به طور خلاصه این اصل میگه اگر تایپ B از تایپ A ارث بری میکنه، هرجایی که ما نمونه ای از تایپ A داشته باشیم باید بتونیم به جاش نمونه ای از تایپ B رو قرار بدیم بدون اینکه برنامه به مشکل بخوره. در غیر این صورت این رابطه ارث بری صحیح نیست.

مثال: فرض کنید یه کلاس base داریم به اسم Bird که یک متد داره به اسم Fly:
class Bird:
def fly(self):
print("I can fly...")
و دو تا subclass داریم به اسم Duck (اردک) و Ostrich (شترمرغ) که از Bird ارث بری میکنن:

class Duck(Bird):
pass

class Ostrich(Bird):
pass
الان اردک میتونه پرواز کنه چون یه پرنده هست، ولی آیا شترمرغ میتونه پرواز کنه؟ نه نمیتونه. آیا پرنده نیست؟ چرا هست. پس این رابطه درست نیست و اصل LSP رو رعایت نمیکنه.

رابطه ی صحیح چطور میتونه باشه؟ اینکه تشخیص بدیم اصلا Fly متد درستی برای Bird نبوده و اون رو این شکلی بازنویسی کنیم:
class Bird:
pass

class FlyingBirds(Bird):
def fly(self):
print("I can fly...")

class Duck(FlyingBirds):
pass

class Ostrich(Bird):
pass

✒️ @Amirsoroushh

BY 🐍 Python & Raspberry 🐍


Share with your friend now:
tgoop.com/raspberry_python/6502

View MORE
Open in Telegram


Telegram News

Date: |

Telegram message that reads: "Bear Market Screaming Therapy Group. You are only allowed to send screaming voice notes. Everything else = BAN. Text pics, videos, stickers, gif = BAN. Anything other than screaming = BAN. You think you are smart = BAN. The visual aspect of channels is very critical. In fact, design is the first thing that a potential subscriber pays attention to, even though unconsciously. How to Create a Private or Public Channel on Telegram? Although some crypto traders have moved toward screaming as a coping mechanism, several mental health experts call this therapy a pseudoscience. The crypto community finds its way to engage in one or the other way and share its feelings with other fellow members. How to Create a Private or Public Channel on Telegram?
from us


Telegram 🐍 Python & Raspberry 🐍
FROM American