static void Main(string[] args) { Console.WriteLine("***** Fun with Polymorphism *****\n"); Hexagon hex = new Hexagon("Beth"); hex.Draw(); Circle cir = new Circle("Cindy"); // Calls base class implementation! cir.Draw(); // Make an array of Shape-compatible objects. Shape[] myShapes = { new Hexagon(), new Circle(), new Hexagon("Mick"), new Circle("Beth"), new Hexagon("Linda") }; // Loop over each item and interact with the // polymorphic interface. foreach (Shape s in myShapes) { s.Draw(); } // This calls the Draw() method of the ThreeDCircle. ThreeDCircle o = new ThreeDCircle(); o.Draw(); // This calls the Draw() method of the parent! ((Circle)o).Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("*** Fun with abstract and virtual Class/methods ***"); Circle cr = new Circle("Big circle"); cr.Draw(); Hexagon hx = new Hexagon("Little Hexagon"); hx.Draw(); Console.WriteLine(); Shape[] MyShapes = { new Hexagon(), new Circle(), new Hexagon("Little"), new Circle("Big") }; foreach (Shape i in MyShapes) { i.Draw(); } Console.WriteLine(); ThreeDCircle o = new ThreeDCircle(); o.Draw(); ((Circle)o).Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("**** Fun with Polymorphism ****\n"); Hexagon hex = new Hexagon("Beth"); hex.Draw(); Circle cir = new Circle("Cindy"); cir.Draw(); Console.WriteLine("-----------------------------------"); //Создать массив совместимых с Shape объектов Shape[] myShapes = { new Hexagon(), new Circle(), new Hexagon("Mick"), new Circle("Beth"), new Hexagon("Linda") }; //Пройти цикл по каждому элементу и взаимодействовать с полиморфным интерфейсом foreach (Shape s in myShapes) { s.Draw(); } Console.WriteLine("-----------------------------------"); //Здесь вызывается метод Draw из класса ThreeDCircle ThreeDCircle o = new ThreeDCircle(); o.Draw(); //Здесь вызывается метод Draw() родителя! ((Circle)o).Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Polymorphism *****\n"); Hexagon hex = new Hexagon("Beth"); hex.Draw(); Console.WriteLine(); Circle cir = new Circle("Cindy"); // Calls base class implementation cir.Draw(); Console.WriteLine(); Shape[] myShapes = { new Hexagon(), new Circle(), new Hexagon("Mick Jaeger"), new Circle("Linda"), new Hexagon("Mc Beth") }; foreach (Shape s in myShapes) { s.Draw(); } Console.WriteLine(); ThreeDCircle d = new ThreeDCircle(); ((Circle)d).Draw(); d.Draw(); Console.ReadLine(); }
public static void Main(string[] args) { Console.WriteLine(" Understanding Polymorphic Interface"); Hexagon hex = new Hexagon("Beth"); hex.Draw(); Circle cir = new Circle("Cindy"); cir.Draw(); // Make an array of Shape-compatible Objects Shape[] myShapes = { new Hexagon(), new Circle(), new Hexagon("Mick") }; //Loop over each item and interact with the polymorphic interface foreach (Shape s in myShapes) { s.Draw(); } // Possibility to trigger the base class implementation of a shadowed member // using explict cast // This calls shadowed memeber ThreeDCircle o = new ThreeDCircle(); o.Draw(); // This calls the Draw method of the Parent ((Circle)o).Draw(); }
static void Main(string[] args) { Console.WriteLine("**** Fun with polymorphysm ****"); Hexagon betty = new Hexagon("Betty"); betty.Draw(); Circle nikky = new Circle("Nikky"); nikky.Draw(); Console.WriteLine(); Shape[] myShapes = { new Circle(), new Hexagon(), new Hexagon("Nikky"), new Circle("Betty"), new Hexagon("Jack") }; foreach (Shape s in myShapes) { s.Draw(); } Console.WriteLine(); ThreeDCircle o = new ThreeDCircle(); o.Draw(); ((Circle)o).Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Polymorphism *****\n"); Hexagon hex = new Hexagon("Beth"); hex.Draw(); Circle cir = new Circle(name: "Cindy"); // Calls base class implementation! cir.Draw(); Shape[] myShapes = { new Hexagon(), new Circle(), new Hexagon("Mick"), new Circle("Beth"), new Hexagon("Linda") }; foreach (Shape s in myShapes) { s.Draw(); } ThreeDCircle o = new ThreeDCircle(); o.Draw(); // This calls the Draw() method of the parent! ((Circle)o).Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Polymorphism *****"); Hexagon hex = new Hexagon("Beth"); hex.Draw(); Circle cir = new Circle("Cidny"); cir.Draw(); //Создадим массив объектов типа Shape Shape[] myShapes = { new Hexagon(), new Circle(), new Hexagon("Mick"), new Circle("Beth"), new Hexagon("Linda") }; //Пройдем по всем элементам массива и //взаимодействуем с полиморфным массивом foreach (Shape shape in myShapes) { shape.Draw(); } ThreeDCircle o = new ThreeDCircle(); //Вызываем метод, определенный в ThreeDCircle o.Draw(); //Вызываем метод, определенный в родительском классе, при помощи явного приведения! ((Circle)o).Draw(); Console.ReadLine(); }
static void Abstract() { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("=> Abstract"); Hexagon hex = new Hexagon("Beth"); hex.Draw(); Circle cir = new Circle("Cindy"); cir.Draw(); }
static void Main(string[] args) { Hexagon hex = new Hexagon("Beth"); hex.Draw(); Circle cir = new Circle("Cindy"); // Calls base class implementation cir.Draw(); // Make an array of Shape-compatible objects Shape[] myShapes = {new Hexagon(), new Circle(), new Hexagon("Mick") , new Circle("Beth"), new Hexagon("Linda")}; // Loop over each item and interact with the polymorphic interface foreach (Shape s in myShapes) { s.Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Polymorphism *****\n"); Hexagon hex = new Hexagon("Beth"); hex.Draw(); Circle cir = new Circle("Cindy"); // Calls base class implementation! cir.Draw(); Console.WriteLine(); // Make an array of Shap-compatible objects. Shape[] myShapes = {new Hexagon(), new Circle(), new Hexagon("Mick"), new Circle("Beth"), new Hexagon("Linda")}; // Loop over each item and interact with // Polymorphic interface. foreach (Shape s in myShapes) { s.Draw(); } Console.WriteLine(); // This calls the Draw() method of the ThreeDCircle. ThreeDCircle o = new ThreeDCircle(); o.Draw(); DrawCircle(o); Console.WriteLine(); // This calls the Draw() method of the parent! ((Circle)o).Draw(); Circle oCopy = o as Circle; oCopy.Draw(); Console.WriteLine(); Console.ReadKey(); }
static void Main(string[] args) { Hexagon hex = new Hexagon("Beth"); hex.Draw(); Circle cir = new Circle("Cindy"); cir.Draw(); Shape[] myShapes = { new Hexagon(), new Circle(), new Hexagon("Mick"), new Circle("Beth"), new Hexagon("Linda") }; foreach (Shape s in myShapes) { s.Draw(); } ThreeDCircle o = new ThreeDCircle(); o.Draw(); ((Circle)o).Draw(); Console.ReadLine(); }
static void Main(string[] args) { Hexagon hex = new Hexagon("Beth"); hex.Draw(); Circle cir = new Circle("Cindy"); cir.Draw(); Console.WriteLine(); Shape[] myShapes = { new Hexagon(), new Circle(), new Hexagon("Mick"), new Circle("Beth"), new Hexagon("Linda") }; foreach (Shape s in myShapes) { s.Draw(); } Console.WriteLine(); ThreDCircle c = new ThreDCircle(); c.Draw(); //calls method form 3d circle Circle c1 = new ThreDCircle(); c1.Draw(); //calls method form circle Console.ReadLine(); }
static void Main(string[] args) { // Note: Cannot create an instance of the abstract class or interface "shape": // Shape abstShape = new Shape() { }; Console.WriteLine("***** Fun with Polymorphism *****\n"); // make an array of Shape-compatible objects Shape[] myShapes = { new Hexagon(), new Circle(), new Hexagon("Mick"), new Circle("Beth"), new Hexagon("Linda") }; // Loop over each item (above) and interact with the polymorphic interface foreach (Shape s in myShapes) { s.Draw(); } Console.WriteLine(); Console.ReadLine(); Console.WriteLine(); Hexagon hex = new Hexagon("Beth"); hex.Draw(); // calls Hexagon.Draw() method() Circle cir = new Circle("Cindy"); cir.Draw(); // calls base class implementation // it is still possible to trigger the base class implementation of a shadowed member // using an explicit cast: // Example: This calls the Draw() method of the ThreeDCircle: _3dCircle o = new _3dCircle(); o.Draw(); // Example: This calls the Draw() method of the parent, Circle ((Circle)o).Draw(); // Pause program execution before exit Console.WriteLine(); Console.ReadLine(); }