Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu tuannguyenssu/OcpBefore.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
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
You can’t perform that action at this time.