Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu

tuannguyenssu/LSP2Before.cs Secret

Last active Jul 7, 2019
Embed
What would you like to do?
public class Rectangle
{
public int Height { get; set; }
public int Width { get; set; }
public virtual void SetHeight(int height)
{
this.Height = height;
}
public virtual void SetWidth(int width)
{
this.Width = width;
}
public virtual void PrintArea()
{
Console.WriteLine(this.Height * this.Width);
}
}
public class Square : Rectangle
{
public override void SetHeight(int height)
{
this.Height = height;
this.Width = height;
}
public override void SetWidth(int width)
{
this.Height = width;
this.Width = width;
}
}
Rectangle rect = new Rectangle();
rect.SetHeight(10);
rect.SetWidth(5);
rect.PrintArea(); // Kết quả là 5 * 10
// Below instantiation can be returned by some factory method
Rectangle rect1 = new Square();
rect1.SetHeight(10);
rect1.SetWidth(5);
rect1.PrintArea();
// Kết quả là 5 x 5. Nếu đúng phải là 10x5, vì diện tích 1 hình chữ nhật là dài x rộng
// Class Square sửa hành vi của class cha Rectangle, set cả dài và rộng về 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment