Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp1
- {
- public abstract class Shape
- {
- // Shape properties
- public double X { get; private set; }
- public double Y { get; private set; }
- // Virtual method
- public virtual void Draw()
- {
- Console.WriteLine("Shape: Draw()");
- }
- }
- class Circle : Shape
- {
- public double radius { get; private set; }
- public Circle(double radius)
- {
- this.radius = radius;
- }
- public override void Draw()
- {
- Console.WriteLine("Circle: Draw() - Circumference: " + Circumference());
- base.Draw();
- }
- public double Circumference()
- {
- return (2 * this.radius) * Math.PI;
- }
- }
- class Rectangle : Shape
- {
- public double height { get; private set; }
- public double width { get; private set; }
- public Rectangle(double height, double width)
- {
- this.height = height;
- this.width = width;
- }
- public override void Draw()
- {
- Console.WriteLine("Rectangle: Draw() - Perimiter: " + Perimeter());
- base.Draw();
- }
- public double Perimeter()
- {
- return 2 * (this.height + this.width);
- }
- }
- class Triangle : Shape
- {
- public double a { get; private set; }
- public double b { get; private set; }
- public double c { get; private set; }
- public Triangle(double a, double b, double c)
- {
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public override void Draw()
- {
- Console.WriteLine("Triangle: Draw() - Perimiter: " + Perimeter());
- base.Draw();
- }
- public double Perimeter()
- {
- return this.a + this.b + this.c;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- // Universal Polymorphism at work #1: a Rectangle, Triangle and Circle
- // can all be used whereever a Shape is expected. No cast is
- // required because an implicit conversion exists from a derived
- // class to its base class.
- var shapes = new List<Shape>
- {
- new Rectangle(34.5, 62.0),
- new Triangle(22.1, 36.9, 84.6),
- new Circle(57)
- };
- // Universal Polymorphism at work #2 (Overriding): the virtual method Draw is
- // invoked on each of the derived classes, not the base class.
- foreach (Shape shape in shapes)
- {
- shape.Draw();
- Console.WriteLine();
- }
- // Universal Polymorphism at work #3 (Parametric): the class ParametricPolymorphism defines
- // a generic type that allows the programmer to instantiate an object with whatever generic type
- // he/she chooses. The function ParametricPolymorphism.Add has been written so that it may account
- // for any type that was passed to the generic.
- ParametricPolymorphism<Shape> container = new ParametricPolymorphism<Shape>();
- foreach (Shape shape in shapes)
- {
- container.Add(shape);
- }
- // Ad-Hoc Polymorphism at work #1 (Overloading): we are able to pass in
- // different types and amounts of arguments in our function calls because we have
- // overloaded the function signature "AddNumbers" in our definition file. The One Definition Rule
- // must still not be violated, so the compiler will rename the function to a unique name.
- short f = 4;
- Console.WriteLine(AdHocPolymorphism.AddNumbers(3.5, 3.5));
- Console.WriteLine(AdHocPolymorphism.AddNumbers(1, 2, 3.5));
- Console.WriteLine(AdHocPolymorphism.AddNumbers(1.5F, 2, 3.5));
- // Ad-Hoc Polymorphism at work #2 (Coercion): Despite the arguments types
- // do not correspond with the function parameter types, the program will still compile successfully
- // because the compiler will insert neccessary code to make sure the conversion is successful.
- Console.WriteLine(AdHocPolymorphism.AddNumbers(1L, 1F));
- Console.WriteLine(AdHocPolymorphism.AddNumbers(f, f, 1U));
- Console.WriteLine("Press any key to exit.");
- Console.ReadKey();
- }
- }
- }
- using System.Collections.Generic;
- namespace ConsoleApp1
- {
- class AdHocPolymorphism
- {
- public static double AddNumbers(double x, double y) { return x + y; }
- public static double AddNumbers(int a, int b, double c) { return a + b + c; }
- public static double AddNumbers(float a, int b, double c) { return a + b + c; }
- }
- class ParametricPolymorphism<T>
- {
- List<T> container = new List<T>();
- public void Add(T x)
- {
- this.container.Add(x);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement