Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu tuannguyenssu/OcpAfter.cs Secret
Created Jul 7, 2019

Embed
What would you like to do?
// 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
You can’t perform that action at this time.