Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Polymorphism
- {
- static void Main(string[] args)
- {
- Shape[] figures = new Shape[]
- {
- new Rectangle(3,3),
- new Circle(2),
- new Rectangle(2,3),
- new Circle(5),
- new Triangle(5,7),
- new Triangle(2,3)
- };
- foreach (Shape figure in figures)
- {
- Console.WriteLine("Figure = {0} surface = {1:F2}",
- figure.GetType().Name.PadRight(9, ' '),
- figure.CalculateSurface());
- }
- }
- }
- public abstract class Shape
- {
- private int width;
- private int height;
- public int Width
- {
- get { return this.width; }
- set
- {
- if (this.width < 0)
- {
- throw new ArgumentOutOfRangeException();
- }
- this.width = value;
- }
- }
- public int Height
- {
- get { return this.height; }
- set
- {
- if (this.height < 0)
- {
- throw new ArgumentOutOfRangeException();
- }
- this.height = value;
- }
- }
- public Shape(int width, int height)
- {
- this.Width = width;
- this.Height = height;
- }
- public abstract double CalculateSurface();
- }
- public class Triangle : Shape
- {
- public Triangle(int width, int height)
- : base(width, height)
- {
- }
- public override double CalculateSurface()
- {
- return this.Width * this.Height / 2;
- }
- }
- public class Rectangle : Shape
- {
- public Rectangle(int width, int height)
- : base(width, height)
- {
- }
- public override double CalculateSurface()
- {
- return this.Width * this.Height;
- }
- }
- public class Circle : Shape
- {
- public Circle(int radius)
- : base(radius, height: radius)
- {
- }
- public override double CalculateSurface()
- {
- return Math.PI * this.Width * this.Width;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement