Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu

tuannguyenssu/LSP2After.cs Secret

Created Jul 7, 2019
Embed
What would you like to do?
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