Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu
Created July 7, 2019 07:55
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/f9221fb4c7ea7d242781d7f3e05e3a94 to your computer and use it in GitHub Desktop.
Save tuannguyenssu/f9221fb4c7ea7d242781d7f3e05e3a94 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
// Chuyển logic tính diện tích vào mỗi class
public abstract class Shape
{
public double Area();
}
public class Square : Shape
{
public double Height { get; set; }
public double Area() {
return Math.Sqrt(this.Height);
}
}
public class Circle : Shape
{
public double Radius { get; set; }
public double Area() {
return this.Radius * this.Radius * Math.PI;
}
}
public class Triangle : Shape
{
public double FirstSide { get; set; }
public double SecondSide { get; set; }
public double ThirdSide { get; set; }
public double Area() {
double TotalHalf = (this.FirstSide + this.SecondSide + this.ThirdSide) / 2;
var area += Math.Sqrt(TotalHalf * (TotalHalf - this.FirstSide) *
(TotalHalf - this.SecondSide) * (TotalHalf - this.ThirdSide));
return area;
}
}
// 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) {
Console.WriteLine(shape.Area());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment