Friday, December 26, 2014

Polymorphism

using System;

abstract class Figure
{
    public abstract double CalcSurface();
}


using System;

class Circle : Figure
{
    public double Radius { get; set; }
   
    public override double CalcSurface()
    {
        return Math.PI * this.Radius * this.Radius;
    }
}

using System;

class Rectangle : Figure
{
    public double Width { get; set; }
    public double Height { get; set; }
   
    public override double CalcSurface()
    {
        return this.Width * this.Height;
    }
}

using System;

class Square : Figure
{
    public double Size { get; set; }
   
    public override double CalcSurface()
    {
        return this.Size * this.Size;
    }
}


using System;

class Polymorphism
{
    static void Main()
    {
        Figure[] figures = new Figure[] {
            new Square() { Size = 3 },
            new Circle() { Radius = 2 },
            new Rectangle() { Width = 2, Height = 3 },
            new Circle() { Radius = 3.5 },
            new Square() { Size = 2.5 },
            new Square() { Size = 4 },
        };

        foreach (Figure figure in figures)
        {
            Console.WriteLine("Figure = {0} surface = {1:F2}",
                figure.GetType().Name.PadRight(9,' '),
                figure.CalcSurface());
            Console.ReadLine();
        }
    }
}

OUTPUT

Figure = Square    surface = 9.00

Figure = Circle    surface = 12.57

Figure = Rectangle surface = 6.00

Figure = Circle    surface = 38.48

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...