Advertisement
NadiaGeorgieva

Working with List && OOP

May 26th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. class Program
  2. {  
  3.   abstract class Shape
  4.     {
  5.         public string Name { get; set; }
  6.         public string Color { get; set; }
  7.         public abstract double GetPerimeter();
  8.         public Shape(string name,string color)
  9.         {
  10.             this.Name = name;
  11.             this.Color = color;
  12.         }
  13.  
  14.     }
  15.      class MyCircle : Shape
  16.     {
  17.         public double Radius { get; set; }
  18.         public MyCircle(string name,string color,double radius)
  19.             :base(name,color)
  20.         {
  21.             this.Radius = radius;
  22.         }
  23.         public override double GetPerimeter()
  24.         {
  25.             return Math.PI * Radius * 2;
  26.         }
  27.     }
  28.  
  29.     class MyRectangle : Shape
  30.     {
  31.         public double A { get; set; }
  32.         public double B { get; set; }
  33.         public MyRectangle(string name,string color,double a,double b)
  34.             :base(name,color)
  35.         {
  36.             this.A = a;
  37.             this.B = b;
  38.         }
  39.         public override double GetPerimeter()
  40.         {
  41.             return (A + B) * 2;
  42.         }
  43.     }
  44.         public double A { get; set; }
  45.         public double B { get; set; }
  46.         public double C { get; set; }
  47.         public MyTriangle(string name,string color,double a,double b,double c)
  48.             :base(name,color)
  49.         {
  50.             this.A = a;
  51.             this.B = b;
  52.             this.C = c;
  53.         }
  54.         public override double GetPerimeter()
  55.         {
  56.             return A + B + C;
  57.         }
  58.             static void Main(string[] args)
  59.            {
  60.                 MyCircle circle = new MyCircle("circle1","green",2);
  61.                 MyRectangle rectangle = new MyRectangle("rectangle1","black",4,2);
  62.                 MyTriangle triangle = new MyTriangle("triangle1","red",2,4,9);
  63.                 List<Shape> shapes = new List<Shape>();
  64.                 shapes.Add(circle);
  65.                 shapes.Add(rectangle);
  66.                 shapes.Add(triangle);
  67.                  foreach (Shape item in shapes)
  68.                 {
  69.                     Console.WriteLine(item.Name+", "+item.Color+", "+item.GetPerimeter());
  70.                 }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement