Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu
Created July 7, 2019 07:53
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/0df7be81f76d410bf2169e739fd9132a to your computer and use it in GitHub Desktop.
Save tuannguyenssu/0df7be81f76d410bf2169e739fd9132a to your computer and use it in GitHub Desktop.
// Ta có 3 class: vuông, tròn, tam giác, kế thừa class Shape
public class Shape
{
}
public class Square : Shape
{
public double Height { get; set; }
}
public class Circle : Shape
{
public double Radius { get; set; }
}
public class Triangle : Shape
{
public double FirstSide { get; set; }
public double SecondSide { get; set; }
public double ThirdSide { get; set; }
}
// Module in ra diện tích các hình
public class AreaDisplay
{
public double ShowArea(List<Shape> shapes)
{
foreach (var shape in shapes) {
// Nếu yêu cầu thay đổi, thêm shape khác, ta phải sửa module Area Calculator
if (shape is Square) {
Square square = (Square)shape;
var area += Math.Sqrt(square.Height);
Console.WriteLine(area);
}
if (shape is Triangle) {
Triangle triangle = (Triangle)shape;
double TotalHalf = (triangle.FirstSide + triangle.SecondSide + triangle.ThirdSide) / 2;
var area += Math.Sqrt(TotalHalf * (TotalHalf - triangle.FirstSide) *
(TotalHalf - triangle.SecondSide) * (TotalHalf - triangle.ThirdSide));
Console.WriteLine(area);
}
if (shape is Circle) {
Circle circle = (Circle)shape;
var area += circle.Radius * circle.Radius * Math.PI;
Console.WriteLine(area);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment