Like a combo of Interfaces and Classes
Given the following Classes
public class GeometryTool { public static void Run() { // fields var square = new Square() { Width = 2 }; var triangle = new Triangle() { Base = 2, Height = 5 }; Console.WriteLine(square.GetArea()); Console.WriteLine(triangle.GetArea()); } } class Square { public int Width; public int GetArea() { return Width * Width; } } class Triangle { public int Base; public int Height; public int GetArea() { return (Base * Height)/ 2; } }
Creating a shape abstract class, allows for merging similar function names and using override methods
Interfaces have no logic whereas Abstract Classes an implemented functions
public class GeometryTool { public static void Run() { // fields var square = new Square() { Width = 2 }; var triangle = new Triangle() { Base = 2, Height = 5 }; square.Display(); triangle.Display(); } } abstract class Shape { public abstract int GetArea(); public void Display() { Console.WriteLine("The area is {0}", GetArea()); } } class Square : Shape { public int Width; public override int GetArea() { return Width * Width; } } class Triangle : Shape { public int Base; public int Height; public override int GetArea() { return (Base * Height)/ 2; } }
- Log in to post comments
Tags