Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu
Last active July 7, 2019 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuannguyenssu/d5749ed0cf0d5be4783a81180fe8d933 to your computer and use it in GitHub Desktop.
Save tuannguyenssu/d5749ed0cf0d5be4783a81180fe8d933 to your computer and use it in GitHub Desktop.
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