예제 #1
0
        static void Main(string[] args)
        {
            #region Array of base class type created and intiialized with
            // a member of each class in the hierarchy
            ParentClass[] myArray = new ParentClass[3];
            myArray[0] = new ParentClass();
            myArray[1] = new ChildClass();
            myArray[2] = new GrandChildClass();

            // Show that a method belonging to the base class is called from a child instance
            // create ParentMethod() in Parent and iterate through array
            // calling this method to demonstrate that all child classes
            // have access to the ParentMethod()
            foreach (ParentClass element in myArray)
            {
                Console.Write("{0}: ", element.GetType());
                element.ParentMethod();
            }
            Console.WriteLine();
            #endregion

            #region Demonstrate overriding a base class method by a child
            // demonstrate a child method calling the parent's method
            foreach (ParentClass element in myArray)
            {
                Console.Write("{0}: ", element.GetType());
                element.ParentMethod();
            }
            Console.WriteLine();
            #endregion

            #region Demonstrate a child method calling the parent's method
            // create a virtual method in parent
            // override in child and call the parent as part of the implementation.
            foreach (ParentClass element in myArray)
            {
                Console.Write("{0}: ", element.GetType());
                element.Method2();
                Console.WriteLine();
            }
            #endregion

            #region Demonstrate use of "as" keyword
            // repeat calls to SomeMethod
            foreach (object element in myArray)
            {
                // casting and creating new variable using as
                ParentClass pc = element as ParentClass;
                // below is the explicit cast not using as
                //ParentClass pc = (ParentClass)element;
                Console.Write("{0}: ", pc.GetType());
                pc.SomeMethod();
            }
            Console.WriteLine();
            #endregion

            #region Demonstrate use of "is" keyboard

            Console.WriteLine("Demo use of is:");
            foreach (ParentClass element in myArray)
            {
                if (element is GrandChildClass)
                {
                    Console.WriteLine("GrandChild Class");
                }
                else if (element is ChildClass)
                {
                    Console.WriteLine("Child Class");
                }
                else if (element is ParentClass)
                {
                    Console.WriteLine("Parent Class");
                }
            }
            Console.WriteLine();
            #endregion

            #region Switch with cases
            Console.WriteLine("Demo Switch:");
            foreach (ParentClass element in myArray)
            {
                switch (element)
                {
                case GrandChildClass gc:
                    Console.WriteLine("GrandChild in Switch");
                    break;

                case ChildClass cc:
                    Console.WriteLine("Child Class in Switch");
                    break;

                case ParentClass pc:
                    Console.WriteLine("Parent Class in Switch");
                    break;

                default:
                    Console.WriteLine("default");
                    break;
                }
            }
            Console.WriteLine();

            #endregion

            #region create object of "object" type but intialized of the
            // base class type then cast it to the actual type so you can call
            object obj = new ParentClass();
            // cast ParentClass on obj to call parent method
            ((ParentClass)obj).ParentMethod();

            // cast the grandchild class onto the parent class
            ParentClass p2 = new GrandChildClass();
            ((GrandChildClass)p2).SpecializedMethod();
            Console.WriteLine();
            #endregion
        }
        static void Main(string[] args)
        {
            #region Array of base class type
            // array of base class
            ParentClass[] myArray = new ParentClass[3];
            myArray[0] = new ParentClass();
            myArray[1] = new ChildClass();
            myArray[2] = new GrandChildClass();

            // print array using a foreach
            Console.WriteLine("Print ParentClass Array: ");
            foreach (ParentClass entry in myArray)
            {
                Console.WriteLine("{0}: ", entry.GetType());
                entry.ParentClassMethod();
            }
            Console.WriteLine();
            #endregion

            #region switch and foreach print
            // foreach with switch, prints based on class type
            Console.WriteLine("Foreach with switch: ");
            foreach (ParentClass entry in myArray)
            {
                switch (entry)
                {
                case GrandChildClass x:
                    Console.WriteLine("GrandChildClass");
                    break;

                case ChildClass b:
                    Console.WriteLine("ChildClass");
                    break;

                case ParentClass c:
                    Console.WriteLine("ParentClass");
                    break;

                default:
                    Console.WriteLine("Default.");
                    break;
                }
            }
            Console.WriteLine();
            #endregion

            #region call parent method in a foreach loop through the array
            // uses as keyword and calls parent class from child class
            Console.WriteLine("Print foreach loop based on class:");
            foreach (ParentClass entry in myArray)
            {
                ChildClass      c1 = entry as ChildClass;
                GrandChildClass g1 = entry as GrandChildClass;
                ParentClass     p1 = entry as ParentClass;
                if (c1 == null)
                {
                    Console.Write("{0} is not a child class: ",
                                  entry.GetType());
                    entry.ParentClassMethod();
                }

                else
                {
                    Console.Write("{0} is a child class that can call: ",
                                  entry.GetType());
                    entry.ParentClassMethod();
                }
            }
            Console.WriteLine();
            #endregion

            #region create object of parentclass demonstrate is
            Console.WriteLine("Identify Child or Parent from myArray:");
            foreach (object o in myArray)
            {
                // if-else with is: children print trap line, else gets types
                if (o is ChildClass)
                {
                    Console.Write("Child class: ");
                    Console.WriteLine(o.GetType());
                }
                else
                {
                    Console.Write("Parent Class: ");
                    Console.WriteLine(o.GetType());
                }
            }
            Console.WriteLine();
            #endregion

            #region creates new instances of classes
            ParentClass pc   = new ParentClass();
            ChildClass  cc   = new ChildClass();
            ParentClass pccc = new ChildClass();

            // cast the grandchild class onto the parent class
            ParentClass p2 = new GrandChildClass();
            ((GrandChildClass)p2).ParentClassMethod();
            Console.WriteLine();

            // calls the parent method using the child method
            Console.WriteLine("Children calling the Parent Method:");
            pc.ParentClassMethod();
            cc.ParentClassMethod();
            pccc.ParentClassMethod();
            Console.WriteLine();

            // array of objects, with object set to parent class
            object[] AnotherArray = new object[3];
            AnotherArray[0] = pc;
            AnotherArray[1] = cc;
            AnotherArray[2] = pccc;

            Console.WriteLine("AnotherArray has pc {0}, cc {1}, and pccc {2}.",
                              AnotherArray);
            Console.WriteLine();
            #endregion
        }