123
1 2 |
IPointy[] myPointObjs = { new Hexagon(), new Knife(), new Triangle() }; foreach (var i in myPointObjs){ Console.WriteLine(i.Points); } |
full example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Interace { class Program { static IPointy FindFirstPointyShape(Shape[] shapes) { foreach (var s in shapes) { if (s is IPointy) { return s as IPointy; } } return null; } static void Main(string[] args) { IPointy[] myPointObjs = { new Hexagon(), new Knife(), new Triangle() }; foreach (var i in myPointObjs){ Console.WriteLine(i.Points); } Shape[] shapes = { new Triangle("Triangle"), new Hexagon("Hexagon"), new Circle("Circle") }; IPointy firstPointyItem = FindFirstPointyShape(shapes); Console.WriteLine("Item has {0} points", firstPointyItem.Points); Triangle t = new Triangle("Triangle"); t.Draw(); Hexagon h = new Hexagon("Hexagon"); h.Draw(); Circle c = new Circle("Circle"); c.Draw(); // another example for (int i = 0; i < shapes.Length; i++) { if (shapes[i] is IPointy) Console.WriteLine("Points in " + shapes[i].Name + " " + (shapes[i] as IPointy).Points); else Console.WriteLine("Ipointy not supported in " + shapes[i].Name); } Circle c1 = new Circle("Circle"); IPointy itf = null; try { itf = (IPointy)c1; } catch (InvalidCastException e) { Console.WriteLine(e.Message); } Console.ReadLine(); } // base class public class Shape { public string Name { get; set; } public Shape(string name) { Name = name; } public Shape() { } public virtual void Draw() { } } //declaring interface public interface IPointy { byte Points { get; } } // another one public interface IDraw3D { void Draw3D(); } // Triangle - derived from shape and implementing IPointy public class Triangle : Shape, IPointy { public Triangle() { } public Triangle(string name) : base(name) { } public override void Draw() { Console.WriteLine("Shape name is {0}", this.Name); } public byte Points { get { return 3; } } } // Hexagon - derived from shape and implementing IPointy public class Hexagon : Shape, IPointy, IDraw3D { public Hexagon() { } public Hexagon(string name) : base(name) { } public override void Draw() { Console.WriteLine("Shape name is {0}", this.Name); } public byte Points { get { return 6; } } // << another implementation of IPointy public void Draw3D() { Console.WriteLine("Drawing Hexagon in 3D"); } } // // Circle - derived from shape and NOT implementing IPointy public class Circle : Shape { public Circle() { } public Circle(string name) : base(name) { } public override void Draw() { Console.WriteLine("Shape name is {0}", this.Name); } } public class ThreeDCircle : Circle, IDraw3D { public void Draw3D() { Console.WriteLine("Draw Circle in 3D"); } } // knife public class Knife : Shape, IPointy { public Knife() { } public Knife(string name) : base(name) { } public override void Draw() { Console.WriteLine("Shape name is {0}", this.Name); } public byte Points { get { return 4; } } // << another implementation of IPointy } // fork public class Fork : Shape, IPointy { public Fork() { } public Fork(string name) : base(name) { } public override void Draw() { Console.WriteLine("Shape name is {0}", this.Name); } public byte Points { get { return 1; } } // << another implementation of IPointy } } } |