Last active
August 29, 2015 14:09
-
-
Save dibikhin/265ea5853462fa15fc1d to your computer and use it in GitHub Desktop.
Polymorphism & inheritance sample for LinqPad, http://www.linqpad.net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
IAreaComputable circle = new Сircle(5); | |
IAreaComputable square = new Square(5); | |
Quadrilateral rhombus = new Rhombus(5, 7); | |
circle.ComputeArea().Dump(); | |
square.ComputeArea().Dump(); | |
((IAreaComputable)rhombus).ComputeArea().Dump(); | |
} | |
// Abstracts | |
interface IAreaComputable { | |
double ComputeArea(); | |
} | |
abstract class Shape : IAreaComputable { | |
abstract public double ComputeArea(); | |
} | |
abstract class Quadrilateral : Shape { | |
abstract public override double ComputeArea(); | |
} | |
// Shapes | |
sealed class Сircle: Shape { | |
int _radius; | |
public Сircle(int radius) { | |
_radius = radius; | |
} | |
public override double ComputeArea() { | |
return Math.PI * _radius * _radius; | |
} | |
} | |
sealed class Square: Quadrilateral { | |
int _side; | |
public Square(int side) { | |
_side = side; | |
} | |
public override double ComputeArea() { | |
return _side * _side; | |
} | |
} | |
sealed class Rhombus: Quadrilateral { | |
int _length; | |
int _height; | |
public Rhombus(int length, int height) { | |
_length = length; | |
_height = height; | |
} | |
public override double ComputeArea() { | |
return _length * _height; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment