Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu
Created 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/3ac8ad4d4929dbd2841e10f0ab19a098 to your computer and use it in GitHub Desktop.
Save tuannguyenssu/3ac8ad4d4929dbd2841e10f0ab19a098 to your computer and use it in GitHub Desktop.
public class Shape
{
public virtual void PrintArea()
{
}
}
public class Rectangle : Shape
{
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 override void PrintArea()
{
Console.WriteLine(this.Height * this.Width);
}
}
public class Square : Shape
{
public int Edge { get; set; }
public virtual void SetEdge(int edge)
{
this.Edge = edge;
}
public override void PrintArea()
{
Console.WriteLine(Math.Pow(this.Edge, 2));
}
}
var rect = new Rectangle();
rect.SetHeight(10);
rect.SetWidth(5);
System.Console.WriteLine(rect.PrintArea()); // Kết quả là 50
var square = new Square();
rect.SetEdge(10);
System.Console.WriteLine(rect.PrintArea()); // Kết quả là 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment