اصول طراحی شی گرا SOLID - #بخش سوم اصل LSP
نویسنده: ناصر طاهری
تاریخ: ۱۳۹۲/۰۷/۰۶ ۲۰:۴۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }
}
public class Square:Rectangle
{
//codes specific to
//square will be added
} Rectangle o = new Rectangle(); o.Width = 5; o.Height = 6;
Rectangle o = new Square(); o.Width = 5; o.Height = 6;
public class Square : Rectangle
{
public override int Width
{
get{return base.Width;}
set
{
base.Height = value;
base.Width = value;
}
}
public override int Height
{
get{return base.Height;}
set
{
base.Height = value;
base.Width = value;
}
}
} public abstract class Shape
{
public virtual int Width { get; set; }
public virtual int Height { get; set; }
} Shape o = new Rectangle(); o.Width = 5; o.Height = 6; Shape o = new Square(); o.Width = 5; //both height and width become 5 o.Height = 6; //both height and width become 6
public class Rectangle : Shape
{
//شما میتوانید خاصیتها طول و عرض در کلاس پایه را در صورت نیاز دوباره نویسی کنید
} public class Square : Shape
{
//دوباره نویسی کردن خاصیتهای طول و عرض در کلاس پایه جهت برابر کردن طول و عرض مربع
public override int Width
{
get{return base.Width;}
set
{
base.Height = value;
base.Width = value;
}
}
public override int Height
{
get{return base.Height;}
set
{
base.Height = value;
base.Width = value;
}
}
} Shape o = new Rectangle(); o.Width = 5; o.Height = 6; Shape o = new Square(); o.Width = 5; //طول و عرض هر دو برابر 5 میشوند o.Height = 6; //عرض و طول هر دو برابر 6 میشوند