sashomaga

Polymorphism

Mar 8th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. static void Main()
  2.     {
  3.         try
  4.         {          
  5.             List<Shape> shapes = new List<Shape>();
  6.             shapes.Add(new Triangle(5, 3));
  7.             shapes.Add(new Circle(4, 4));
  8.             shapes.Add(new Rectangle(10, 4));
  9.             //Polymorphism
  10.             foreach (Shape item in shapes)
  11.             {
  12.                 Console.WriteLine(string.Format("Surface : {0} -> {1}",item.GetType(),item.CalculateSurface()));
  13.             }
  14.         }
  15.         catch (ArgumentException err)
  16.         {
  17.             Console.WriteLine(err.Message);
  18.         }
  19.     }
  20.  
  21. public abstract class Shape
  22. {
  23.     //fields
  24.     private double width;
  25.     private double height;
  26.     //properties
  27.     public double Width
  28.     {
  29.         get { return this.width; }
  30.         private set
  31.         {
  32.             if (value <= 0)
  33.             {
  34.                 throw new ArgumentException("Value must be positive!");
  35.             }
  36.             this.width = value;
  37.         }
  38.     }
  39.     public double Height
  40.     {
  41.         get { return this.height; }
  42.         private set
  43.         {
  44.             if (value <= 0)
  45.             {
  46.                 throw new ArgumentException("Value must be positive!");
  47.             }
  48.             this.height = value;
  49.         }
  50.     }
  51.     //abstract method
  52.     public abstract double CalculateSurface();
  53.     //constructor
  54.     protected Shape(double width, double height)
  55.     {
  56.         this.Width = width;
  57.         this.Height = height;
  58.     }
  59. }
  60.  
  61. public class Triangle : Shape
  62. {
  63.     public override double CalculateSurface()
  64.     {
  65.         return this.Height * this.Width / 2d;
  66.     }
  67.  
  68.     public Triangle(double width, double height) : base(width,height)
  69.     {
  70.        
  71.     }
  72. }
  73.  
  74. public class Rectangle : Shape
  75. {
  76.     public override double CalculateSurface()
  77.     {
  78.         return this.Height * this.Width;
  79.     }
  80.  
  81.     public Rectangle(double width, double height)
  82.         : base(width, height)
  83.     {
  84.  
  85.     }
  86. }
  87.  
  88. public class Circle : Shape
  89. {
  90.     public override double CalculateSurface()
  91.     {
  92.         double radius = this.Width / 2d;
  93.         return Math.PI * radius * radius;
  94.     }
  95.  
  96.     public Circle(double width, double height)
  97.         : base(width, height)
  98.     {
  99.         if (width != height)
  100.         {
  101.             throw new ArgumentException("In circle width must be equal to height!");
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment