public static void DoSomethingOnPointy(IPointy pointy) { Console.WriteLine("pointy : {0}", pointy.Name); }
static void PrintNumberOfPoints(IPointy iface) { Console.WriteLine("Число вершин {0}", iface.NumberOfPoints()); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interfaces ******\n"); Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; IPointy firstPointyItem = FindFirstPointyShape(myShapes); Console.WriteLine("The item has {0} points", firstPointyItem.Points); for (int i = 0; i < myShapes.Length; i++) { myShapes[i].Draw(); if (myShapes[i] is IPointy) { Console.WriteLine("-> Points: {0}", ((IPointy)myShapes[i]).Points); } else { Console.WriteLine("-> {0}\'s not pointy!", myShapes[i].PetName); } Console.WriteLine(); if (myShapes[i] is IDraw3D) { DrawIn3D((IDraw3D)myShapes[i]); } } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interfaces *****\n"); // Создать массив элементов Shape. Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; for (int i = 0; i < myShapes.Length; i++) { // Вспомните, что базовый класс Shape определяет абстрактный // член Draw(), поэтому все фигуры знают, как себя рисовать. myShapes[i].Draw(); // У каких фигур есть вершины? if (myShapes[i] is IPointy) { Console.WriteLine("-> Points: {0}", ((IPointy)myShapes[i]).Points); } else { Console.WriteLine("-> {0}\'s notpointy!", myShapes[i].PetName); } // Можно ли нарисовать эту фигуру в трехмерном виде? if (myShapes[i] is IDraw3D) { DrawIn3D((IDraw3D)myShapes[i]); } Console.WriteLine(); } // Получить первый элемент, имеющий вершины. // Для безопасности не помешает проверить firstPointyItem на предмет null. IPointy firstPointyItem = FindFirstPointyShape(myShapes); Console.WriteLine("The item has {0} points", firstPointyItem.Points); Console.ReadLine(); }
static void ExampleReturnInterface() { Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; IPointy firstPointyItem = FindFirstPointyShape(myShapes); Console.WriteLine($"The item has {firstPointyItem.Points} points"); }
static void ExampleInterfaceException() { Circle c = new Circle("Lisa"); IPointy itfPt = null; try { itfPt = (IPointy)c; Console.WriteLine(itfPt.Points); } catch (InvalidCastException e) { Console.WriteLine(e.Message); } Hexagon hex = new Hexagon("Peter"); IPointy itfPt2 = hex as IPointy; if (itfPt2 != null) { Console.WriteLine($"Points: {itfPt2.Points}"); } else { Console.WriteLine("OOPS! Not pointy..."); } }
public static void Main(string[] args) { Console.WriteLine("A first look at Interfaces"); CloneableExample(); // Call Points property defined by IPointy Hexagon hex = new Hexagon(); Console.WriteLine("Points : {0}", hex.Points); // Catch a possible InvalidCastException Circle c = new Circle("Lisa"); IPointy itfPt = null; try { itfPt = (IPointy)c; Console.WriteLine(itfPt.Points); } catch (InvalidCastException e) { Console.WriteLine(e.Message); } // Can we treat hex2 as IPointy Hexagon hex2 = new Hexagon(); IPointy itfPt2 = hex2 as IPointy; if (itfPt2 != null) { Console.WriteLine("Points {0} ", itfPt2.Points); } else { Console.WriteLine("Not implementaed"); } //Obtaining Interface References : The is Keyword Hexagon hex3 = new Hexagon(); if (hex3 is IPointy itfPt3) { Console.WriteLine("Points {0} ", itfPt3.Points); } else { Console.WriteLine("Not implementaed"); } // Square IRegularPointy sq = new Square("Boxy") { NumberOfSides = 4, SideLength = 4 }; sq.Draw(); // }
public static void Main(string[] args) { //Shape[] shapes = new Shape[] { // new Circle("Circle 1"), // new Hexagon("Hexagon 1"), // new Circle("Circle 2") //}; //foreach (Shape item in shapes) //{ // item.Draw(); //} // //Advanced from here down // Shape s = new Circle("Circle 1"); // //Circle c = s;//Error; // //But I do know that the shape is a circle // Circle c = (Circle)s;//Type casting // c.Draw(); // c.PrintRadius(); // s = new Hexagon("Hexagon 1"); // try{ // Circle c2 = (Circle)s;//Run-time error //}catch(Exception ex){ // Console.WriteLine(ex.Message); // } // //Check // if(s is Circle){ // c = (Circle)s; // Console.WriteLine("Shape is a circle"); // c.Draw(); // } // if(s is Hexagon){ // Hexagon h = (Hexagon)s; // Console.WriteLine("Shape is a hexagon"); // h.Draw(); // } ////as operator ////returns the hexagon if s is a hexagon // //returns null if the shape is not a hexagon //Hexagon h1 = s as Hexagon; ////if(h1!=null){ //// h1.Draw(); ////} //h1?.Draw();//if h1 is not null, call to draw //Test the interface IPointy[] pointyShapes = new IPointy[] { new Hexagon("Hexagon1"), new Triangle("Triangle1"), new Hexagon("Hexagon2") }; foreach (IPointy item in pointyShapes) { item.PrintPoints(); //item.Draw();//error => because it doesn't belong to IPointy } }
static void Main(string[] args) { Hexagon hex = new Hexagon(); Console.WriteLine("Points: {0}", hex.Points); Hexagon hex2 = new Hexagon(); IPointy itfPt2 = hex2 as IPointy; if (itfPt2 != null) { Console.WriteLine("Points: {0}", itfPt2.Points); } else { Console.WriteLine("OOPS! Not pointy..."); } Console.WriteLine(); Shape[] shapes = { new Hexagon("MyHex"), new Circle("MyCircle"), new Triangle("Joe"), new Circle("Jojo") }; for (int i = 0; i < shapes.Length; i++) { shapes[i].Draw(); if (shapes[i] is IPointy) { Console.WriteLine("-> Points: {0}", ((IPointy)shapes[i]).Points); } else { Console.WriteLine("-> {0} \'s not pointy!", shapes[i].PetName); Console.WriteLine(); } } Console.WriteLine("************************************************************************************"); Shape[] myShapes = { new Hexagon("ScHex"), new Circle("ScCirc"), new Triangle("ScTrian"), new Circle("AnotherCirc") }; for (int i = 0; i < myShapes.Length; i++) { if (myShapes[i] is IDraw3D) { DrawIn3D((IDraw3D)myShapes[i]); } } Console.WriteLine("************************************************************************************"); Shape[] my2Shapes = { new Hexagon(), new Circle(), new Triangle() }; IPointy firstPointyItem = FindFirstPointyShape(my2Shapes); Console.WriteLine("The item has {0} points", firstPointyItem.Points); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("*** Fun with Interface ***\n"); //Hexagon hex = new Hexagon(); //Console.WriteLine("Points: {0}", hex.Points); //Circle c = new Circle("C"); //IPointy itfPt = null; //try //{ // itfPt = (IPointy)c; // Console.WriteLine(itfPt.Points); //} //catch (InvalidCastException e) //{ // Console.WriteLine(e.Message); // Console.WriteLine(e.Source); // Console.WriteLine(e.StackTrace); // Console.WriteLine(e.TargetSite); // //throw; //} //Hexagon hex2 = new Hexagon("Hexagon"); //IPointy ipnt = hex2 as IPointy; //if (ipnt == null ) // Console.WriteLine("Points: ", ipnt.Points); //else // Console.WriteLine("Not pointy"); Shape[] myShaes = { new Hexagon("Hex"), new Circle("circle"), new Triangle("Triangle"), new Circle("ABCDEF") }; for (int i = 0; i < myShaes.Length; i++) { if (myShaes[i] is IDraw3D) { DrawIn3D((IDraw3D)myShaes[i]); } //myShaes[i].Draw(); //if (myShaes[i] is IPointy ip) //{ // Console.WriteLine("-> Points: {0} ", ip.Points); //} //else //{ // Console.WriteLine("No Points in {0}",myShaes[i].PetName); //} } IPointy firstPointyItem = FindFirstPointyShape(myShaes); Console.WriteLine("The first item has {0} points", firstPointyItem.Points); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interfaces - Interfaces as return values *****\n"); // Make an array of Shapes. Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; // Get first pointy item. // To be safe, you'd want to check firstPointyItem for null before proceeding. IPointy firstPointyItem = FindFirstPointyShape(myShapes); Console.WriteLine("The item has {0} points", firstPointyItem.Points); }
//Signed by Jelier static void Main(string[] args) { /* * Hexagon hex = new Hexagon(); * Console.WriteLine("Points: {0}", hex.Points); * Circle c = new CustomInterface.Circle("Lisa"); * IPointy itfPt = null; * try * { * itfPt = (IPointy)c; * Console.WriteLine(itfPt.Points); * } * catch (InvalidCastException e) * { * Console.WriteLine(e.Message); * } * Hexagon hex2 = new Hexagon("Peter"); * IPointy itfPt2 = hex2 as IPointy; * if (itfPt2 != null) * Console.WriteLine("Points: {0}", itfPt2.Points); * else * Console.WriteLine("OOPS! Not pointy..."); */ Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; IPointy firstPointyItem = FindFirstPointyShape(myShapes); Console.WriteLine("The item has {0} points", firstPointyItem.Points); for (int i = 0; i < myShapes.Length; i++) { myShapes[i].Draw(); if (myShapes[i] is IPointy) { Console.WriteLine("==> Points: {0}", ((IPointy)myShapes[i]).Points); } else { Console.WriteLine("==> {0}'s not pointy!", myShapes[i].PetName); } if (myShapes[i] is IDraw3D) { DrawIn3D((IDraw3D)myShapes[i]); } Console.WriteLine(); } Console.ReadLine(); }
static void Main(string[] args) { // Вызов членов интерфейса на уровне объектов Hexagon hex = new Hexagon("Гексагон"); Console.WriteLine($"Points: {hex.Points}"); // Получение ссылок на интерфейсы: ключевое слово as IPointy pointy = hex as IPointy; if (pointy != null) { Console.WriteLine("Points: {0}", pointy.Points); } else { Console.WriteLine("OOPS! Not pointy..."); } Console.WriteLine(); // Получение ссылок на интерфейсы: ключевое слово is Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; foreach (Shape shape in myShapes) { shape.Draw(); if (shape is IPointy) { Console.WriteLine("-> Points: {0}", ((IPointy)shape).Points); } else { Console.WriteLine("{0}\'s not pointy!", shape.PetName); } if (shape is IDraw3D) { DrawIn3D((IDraw3D)shape); } Console.WriteLine(); } IPointy pointyItem = FindPointyItem(myShapes); if (pointyItem != null) { Console.WriteLine("The item has {0} points", pointyItem.Points); } Console.ReadLine(); }
static void Main(string[] args) { //Console.WriteLine("***** Fun with Intefaces *****\n"); //Hexagon hex = new Hexagon(); //Console.WriteLine("Points: {0}", hex.Points); //Console.ReadLine(); //Circle c = new Circle("Lisa"); //IPointy itfPt = null; //try //{ // itfPt = (IPointy)c; // Console.WriteLine(itfPt.Points); //} //catch (Exception e) //{ // Console.WriteLine(e.Message); //} //Hexagon hex2 = new Hexagon("Peter"); //itfPt = hex2 as IPointy; //if(itfPt != null) // Console.WriteLine("Points: {0}", itfPt.Points); //else // Console.WriteLine("OOPS! Not pointy..."); Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("Jojo") }; for (int i = 0; i < myShapes.Length; i++) { myShapes[i].Draw(); if (myShapes[i] is IPointy ip) { Console.WriteLine("-> Points: {0}", ip.Points); } else { Console.WriteLine("-> {0}\'s not pointy!", myShapes[i].PetName); } if (myShapes[i] is IDraw3D) { DrawIn3D((IDraw3D)myShapes[i]); } Console.WriteLine(); } IPointy firstPointItem = FindFirstPointyShape(myShapes); Console.WriteLine("The item has {0} points", firstPointItem.Points); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interfaces *****\n"); // Make an array of Shapes. Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; for (int i = 0; i < myShapes.Length; i++) { // Recall the Shape base class defines an abstract Draw() // member, so all shapes know how to draw themselves. myShapes[i].Draw(); // Who's pointy? if (myShapes[i] is IPointy) { Console.WriteLine("-> Points: {0}", ((IPointy)myShapes[i]).Points); } else { Console.WriteLine("-> {0}\'s not pointy!", myShapes[i].PetName); } Console.WriteLine(); // Can I draw you in 3D? if (myShapes[i] is IDraw3D) { DrawIn3D((IDraw3D)myShapes[i]); } } // Get first pointy item. // To be safe, you'd want to check firstPointyItem for null before proceeding. IPointy firstPointyItem = FindFirstPointyShape(myShapes); Console.WriteLine("The item has {0} points", firstPointyItem.Points); // This array can only contain types that // implement the IPointy interface. IPointy[] myPointyObjects = { new Hexagon(), new Knife(), new Triangle(), new Fork(), new PitchFork() }; foreach (IPointy i in myPointyObjects) { Console.WriteLine("Object has {0} points.", i.Points); } Console.ReadLine(); }
public int GetCoordonneesNb() { if (this is Coordonnees) { return(1); } else if (this is IPointy) { IPointy pointy = this as IPointy; return(pointy.NbPoints); } return(-1); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interfaces *****\n"); // Call Points property defined by IPointy. Hexagon hex = new Hexagon(); Console.WriteLine("Points: {0}", hex.Points); // Catch a possible InvalidCastException Circle c = new Circle("Lisa"); IPointy itfPt = null; try { itfPt = (IPointy)c; Console.WriteLine(itfPt.Points); } catch (InvalidCastException e) { Console.WriteLine(e.Message); } // Make an array of Shapes. Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; for (int i = 0; i < myShapes.Length; i++) { myShapes[i].Draw(); // Who's Pointy? if (myShapes[i] is IPointy ip) { Console.WriteLine("-> Points: {0}", ip.Points); } else { Console.WriteLine("-> {0}\'s not pointy!", myShapes[i].PetName); } //if (myShapes[i] is IDraw3D) // DrawIn3D((IDraw3D)myShapes[i]); } for (int i = 0; i < myShapes.Length; i++) { // Can I draw you in 3D? } Console.ReadLine(); }
public int Compare(CartoObj x, CartoObj y) { if (x is IPointy && y is IPointy) { IPointy ip1 = x as IPointy; IPointy ip2 = y as IPointy; return(ip1.NbPoints.CompareTo(ip2.NbPoints)); } else { return(-1); } }
public static void CheckAsPointy(Shape s) { Console.WriteLine("\n*** check AS pointy"); IPointy anyPoints = s as IPointy; if (anyPoints == null) { Console.WriteLine($"XX {s.PetName} {s.GetType().Name} does not implement the IPointy Interface."); } else { Console.WriteLine($"=> {s.PetName} has {anyPoints.Points} points."); } }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interfaces *****\n"); // Make an array of Shapes. Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; // Get first pointy item. // To be safe, you'd want to check firstPointyItem for null before proceeding. IPointy firstPointyItem = FindFirstPointyShape(myShapes); Console.WriteLine("The first pointy item has {0} points", firstPointyItem.Points); Console.WriteLine(); for (int i = 0; i < myShapes.Length; i++) { // Recall the Shape base class defines an abstract Draw() // member, so all shapes know how to draw themselves. myShapes[i].Draw(); // Who's pointy? if (myShapes[i] is IPointy ip) { Console.WriteLine("-> Points: {0}", ip.Points); } else { Console.WriteLine("-> {0}\'s not pointy!", myShapes[i].PetName); } // Can I draw you in 3D? if (myShapes[i] is IDraw3D id3d) { //DrawIn3D((IDraw3D)myShapes[i]); DrawIn3D(id3d); } else { Console.WriteLine("Not derived from the 3d interface."); } Console.WriteLine(); } Console.ReadLine(); }
private static void TestWithTheAsKeyword() { // Can we treat hex2 as IPointy? Hexagon hex2 = new Hexagon("Peter"); IPointy itfPt2 = hex2 as IPointy; if (itfPt2 != null) { Console.WriteLine("\nPoints: {0}", itfPt2.Points); } else { Console.WriteLine("\nOooops! That was not pointy"); } }
static void ByAs() { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("=> Using As keyword"); Hexagon h2 = new Hexagon("Peter"); IPointy ip2 = h2 as IPointy; if (ip2 != null) { Console.WriteLine($"Points: {ip2.Points}"); } else { Console.WriteLine("OOPS! Not pointy..."); } }
static void ReturnInterface() { Console.ForegroundColor = ConsoleColor.DarkCyan; Console.WriteLine("=> Interface as Return Values"); Shape[] shapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; IPointy first = FindFirstPointyShape(shapes); if (first == null) { Console.WriteLine("Not found Pointy shape"); } else { Console.WriteLine($"The item {first.GetType().Name} has {first.Points} points"); } }
/// <summary> /// Obtaining Interface References Using As Keyword /// </summary> private void ObtainingInterfaceReferencesUsingAsKeyword() { Console.WriteLine("=> Obtaining Interface References Using As Keyword:"); Hexagon hex2 = new Hexagon("Peter"); IPointy itfPt2 = hex2 as IPointy; if (itfPt2 != null) { Console.WriteLine("Points: {0}", itfPt2.Points); } else { Console.WriteLine("OOPS! Not pointy..."); } Console.WriteLine(); }
/// <summary> /// Interface As Return Value /// </summary> private void InterfaceAsReturnValue() { Console.WriteLine("=> Interface As Return Value: "); Shape[] shapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; IPointy firstPointyItem = FindFirstPointyShape(shapes); Console.WriteLine("The item has {0} points", firstPointyItem.Points); Console.WriteLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interfaces - as keyword *****\n"); // Can we treat hex2 as IPointy? Hexagon hex2 = new Hexagon("Peter"); IPointy itfPt2 = hex2 as IPointy; if (itfPt2 != null) { Console.WriteLine("Points: {0}", itfPt2.Points); } else { Console.WriteLine("OOPS! Not pointy..."); } Console.ReadLine(); Console.WriteLine(); Console.WriteLine("***** Fun with Interfaces - is keyword *****\n"); // Make an array of Shapes. Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; for (int i = 0; i < myShapes.Length; i++) { // Recall the Shape base class defines an abstract Draw() // member, so all shapes know how to draw themselves. myShapes[i].Draw(); // Who's pointy? if (myShapes[i] is IPointy) { Console.WriteLine("-> Points: {0}", ((IPointy)myShapes[i]).Points); } else { Console.WriteLine("-> {0}\'s not pointy!", myShapes[i].PetName); } Console.WriteLine(); Console.ReadLine(); } }
/// <summary> /// Obtaining Interface References Using Explicitly Cast /// </summary> private void ObtainingInterfaceReferencesUsingExplicitlyCast() { Console.WriteLine("=> Obtaining Interface References Using Explicitly Cast: "); Circle c = new Circle("Lisa"); IPointy itfPt = null; try { itfPt = (IPointy)c; Console.WriteLine(itfPt.Points); } catch (InvalidCastException e) { Console.WriteLine(e.Message); } Console.WriteLine(); }
public static void DemoInteface() { IPointy <int> pointy = null; Point <int> newPoint = new Point <int>(10, 20); Rectangle <int> newRect = new Rectangle <int>("newRect", 20, 30); pointy = newPoint as IPointy <int>; if (null != pointy) { pointy.Description(); } pointy = newRect as IPointy <int>; if (null != pointy) { pointy.Description(); } }
static void DetermineInterface() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("=> Determine whether a type supports a specific interface"); Circle c = new Circle(); Console.WriteLine($"Circle supports IPointy: <{c is IPointy}>"); Console.WriteLine($"Triangle supports IPointy: <{new Triangle() is IPointy}>"); IPointy ip = null; try { ip = (IPointy)c; Console.WriteLine($"The points is {ip.Points}"); } catch (InvalidCastException e) { Console.WriteLine($"Invalid cast error: {e.Message}"); } }
static void Main(string[] args) { Shape[] shapes = new Shape[4]; shapes[0] = new Hexagon("hex"); shapes[1] = new Circle("cir", 10); shapes[2] = new ThreeDCircle("3D", 20); shapes[3] = new Rectangle("rec"); foreach (Shape s in shapes) { if (s is IPointy) { Console.WriteLine(((IPointy)s).Points); } } IPointy p = FindFirstPointyShape(shapes); PointMe(p); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interfaces *****\n"); // Make an array of Shapes. Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") }; for (int i = 0; i < myShapes.Length; i++) { IPointy firstPointyItem = FindFirstPointyShape(myShapes); Console.WriteLine("The item has {0} points", firstPointyItem.Points); // Recall the Shape base class defines an abstract Draw() // member, so all shapes know how to draw themselves. myShapes[i].Draw(); // Who's pointy? if (myShapes[i] is IPointy ip) { Console.WriteLine("-> Points: {0}", ip.Points); } else { Console.WriteLine("-> {0}\'s not pointy!", myShapes[i].PetName); } if (myShapes[i] is IDraw3D) { DrawIn3D((IDraw3D)myShapes[i]); } IPointy[] myPointyObjects = { new Hexagon(), new Knife(), new Triangle(), new Fork(), new PitchFork() }; foreach (IPointy it in myPointyObjects) { Console.WriteLine("Object has {0} points.", it.Points); } Console.WriteLine(); } Console.ReadLine(); }
static void Main(string[] args) { // Make an array of Shapes. Shape[] s = { new Circle("Ceecee"), new Triangle("Titi"), new Circle("JoJo") }; for (int i = 0; i < s.Length; i++) { // Recall the Shape base class defines an abstract Draw() // member, so all shapes know how to draw themselves. s[i].Draw(); // Who's pointy? if (s[i] is IPointy) { IPointy p = s[i] as IPointy; Console.WriteLine("-> Points: {0}", p.Points); } else { Console.WriteLine("-> {0}\'s not pointy!", s[i].PetName); } } }