コード例 #1
0
        static void Main(string[] args)
        {
            #region An array of the base class type is created and initialized
            // An array of the base class type is created and initialized
            // with a member of each class in the hierarchy
            Parent[] myArray = new Parent[3];
            myArray[0] = new Parent();
            myArray[1] = new Child();
            myArray[2] = new GrandChild();

            // Show that a method belonging to the base class is called from a child instance
            // create ParentMethod() in Parent & iterate thru array calling
            // this method to demonstrate that all child classes have access to it

            // test ParentMethod() by doing a foreach loop

            foreach (Parent element in myArray)
            {
                // GetType property is to get type of an element
                Console.Write("{0}: ", element.GetType());
                // call method for each element in array as it iterates thru the foreach
                element.ParentMethod();
            }
            Console.WriteLine();
            #endregion

            #region Demonstrate overriding a base class method by a child

            foreach (Parent element in myArray)
            {
                // GetType property is to get type of an element
                Console.Write("{0}: ", element.GetType());
                // call method for each element in array as it iterates thru the foreach
                element.SomeMethod();
            }
            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 (Parent element in myArray)
            {
                // GetType property is to get type of an element
                Console.Write("{0}: ", element.GetType());
                // call method for each element in array as it iterates thru the foreach
                element.Method2();
                Console.WriteLine();
            }
            Console.WriteLine();

            #endregion

            #region Demonstrate use of "as" keyword
            // repeat calls to SomeMethod but using the as keyword
            // element is of type object
            foreach (object element in myArray)
            {
                // have to cast object to a parent type to use SomeMethod
                // creating a new variable of type p
                // as well as casting to type element
                // following 2 lines are doing almost same thing
                //Parent p = element as Parent; - cast is implicit bc of "as" operator
                //Parent p = (Parent)element; - explcit cast
                // Using the "as" is a litte more expressive
                Parent p = (Parent)element;
                Console.Write("{0}: ", p.GetType());
                // call method for each element in array as it iterates thru the foreach
                p.SomeMethod();
            }

            #endregion

            #region Demonstrate use of "is" keyword
            Console.WriteLine("\nDemo use of is");
            foreach (Parent element in myArray)
            {
                // grandchild to child to parent order
                // if you reverse the order, the child is-a parent
                // and grandchild is-a parent
                // because all derrived classes have an is-a relationship to parent
                if (element is GrandChild)
                {
                    Console.WriteLine("GrandChild");
                }
                else if (element is Child)
                {
                    Console.WriteLine("Child");
                }
                else if (element is Parent)
                {
                    Console.WriteLine("Parent");
                }
            }
            Console.WriteLine();
            #endregion

            #region switch statement
            Console.WriteLine("Demo switch:");
            foreach (Parent element in myArray)
            {
                switch (element)
                {
                // within the case statement creating a variable
                // can call within the switch statement block on that method
                // for example, scope of p is only within the case Parent p block
                case GrandChild gc:
                    Console.WriteLine("GrandChild");
                    break;

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

                case Parent p:
                    Console.WriteLine("Parent");
                    break;

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

            #region Create an object of "object" type
            //, but initialized of the base class type,
            // then cast it to the actual type so you can call a method
            object obj = new Parent();
            /// make an explicit cast to see the methods that belong to Parent
            /// first part of below is casting, second part after dot is calling to method
            ((Parent)obj).ParentMethod();

            Parent p2 = new GrandChild();
            ((GrandChild)p2).SpecializedMethod();

            #endregion
        }
コード例 #2
0
        static void Main(string[] args)
        {
            //an array of the base class type is created and initialized
            // with a member of each class in the hierarchy

            Parent[] myArray = new Parent[3];
            myArray[0] = new Parent();
            myArray[1] = new Child();
            myArray[2] = new GrandChild();


            // 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 it
            foreach (Parent element in myArray)
            {
                Console.Write("{0}: ", element.GetType());
                element.ParentMethod();
            }
            Console.WriteLine();

            //-----------------------------------------------------

            //Demonstrate overriding a base class method by a child
            //parent-virtualMethod| child-overrideMethod

            foreach (Parent element in myArray)
            {
                Console.Write("{0}: ", element.GetType());
                element.SomeMethod();
            }
            Console.WriteLine();

            //--------------------------------------------------------

            //Demostrate 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 (Parent element in myArray)
            {
                Console.Write("{0}: ", element.GetType());
                element.Method2();
                Console.WriteLine();
            }
            Console.WriteLine();

            //-------------------------------------------------------

            //Demonstrate use of "as" keyword
            //repeat calls to someMethod
            foreach (object element in myArray)
            {
                //Parent p = element as Parent;
                Parent p = (Parent)element;
                Console.Write("{0}: ", p.GetType());
                p.SomeMethod();
            }


            //---------------------------------------------------

            //Demonstrate use of "is" keyword
            Console.WriteLine("Demo use of is");
            foreach (Parent element in myArray)
            {
                if (element is GrandChild)
                {
                    Console.WriteLine("grandChild");
                }
                else if (element is Child)
                {
                    Console.WriteLine("Child");
                }
                else if (element is Parent)
                {
                    Console.WriteLine("Parent");
                }
            }
            Console.WriteLine();


            //------------------------------------------------------

            //switch
            Console.WriteLine("Demo Switch");
            foreach (Parent element in myArray)
            {
                switch (element)
                {
                case GrandChild gc:
                    Console.WriteLine("GrandChild");
                    break;

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

                case Parent p:
                    Console.WriteLine("Parent");
                    break;

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

            //----------------------------------------------------

            //Create an object of"object type, but initialized of the
            //base class type, then cast it to the actual type so you can call a method
            object obj = new Parent();

            ((Parent)obj).ParentMethod();

            Parent p2 = new GrandChild();

            ((GrandChild)p2).SpecializedMethod();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            #region An array of the base class type is created and initialized
            // with a member of each class in the hierarchy

            Parent[] myArray = new Parent[3];
            myArray[0] = new Parent();
            myArray[1] = new Child();
            myArray[2] = new GrandChild();

            //Show that a method belonging to the base class is called from a child instance
            // create ParentMethod() in Parent and iterrate through array
            // calling this method to demonstrate that all child classes
            // inherit from it

            foreach (Parent element in myArray)
            {
                Console.WriteLine("{0}", element.GetType());
                element.ParentMethod();
            }
            Console.WriteLine();


            #endregion

            #region Demonstrate overriding a base class method by a child
            // parent with virtual method
            //child with overrid method

            foreach (Parent element in myArray)
            {
                Console.WriteLine("{0}", element.GetType());
                element.someMethod();
            }
            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 (Parent element in myArray)
            {
                Console.WriteLine("{0}", element.GetType());
                element.Method2();
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.Read();


            #endregion

            #region Demonstrate use of "as" keyword
            // repeat call to  SomeMethod
            foreach (object element in myArray)
            {
                // casting at the same time as creating a new variable

                Parent p = element as Parent;
                // second thing is doing the cast operation
                //Parent p = (Parent)element;

                Console.WriteLine("{0}", element.GetType());
                p.ParentMethod();
            }
            #endregion

            #region Demonstrate the "is" keyword
            Console.WriteLine("Demonstrate use of is");
            foreach (Parent element in myArray)
            {
                if (element is GrandChild)
                {
                    Console.WriteLine("GrandChild");
                }
                else if (element is Child)
                {
                    Console.WriteLine("Child");
                }
                else if (element is Parent)
                {
                    Console.WriteLine("Parent");
                }
            }
            #endregion

            // creating variables in a switch
            #region switch
            foreach (Parent element in myArray)
            {
                switch (element)
                {
                case GrandChild gc:
                    Console.WriteLine("GrandChild");
                    break;

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

                case Parent p:
                    Console.WriteLine("Parent");
                    break;

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

            #region Create an object of "object" type, but initialized of the
            //base class type, then cast it to the actual type so you can call a method

            // explicit casting
            object obj = new Parent();
            ((Parent)obj).ParentMethod();

            Parent p2 = new GrandChild();
            ((GrandChild)p2).SpecializedMethod;



            #endregion
            Console.ReadLine();
        }
コード例 #4
0
        static void Main(string[] args)
        {
            #region array of the base class type is created and initialized
            //with a member of each class in the hierarchy
            Parent[] myArray = new Parent[3];
            myArray[0] = new Parent();
            myArray[1] = new Child();
            myArray[2] = new GrandChild();


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

            #region demonstrate overriding a base class method by a child
            foreach (Parent element in myArray)
            {
                Console.Write("{0}: ", element.GetType());
                element.SomeMethod();
            }
            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 (Parent element in myArray)
            {
                Console.Write("{0}: ", element.GetType());
                element.Method2();
                Console.WriteLine();
            }
            Console.WriteLine();
            #endregion

            #region Demonstrate use of "as" keyword
            //repeat calls to SomeMethod
            foreach (object element in myArray)
            {
                Parent p = element as Parent;
                //Parent p = (Parent)element;
                Console.Write("{0}: ", p.GetType());
                p.SomeMethod();
            }
            Console.WriteLine();

            #endregion
            #region Demonstrate use of "is" keyword
            Console.WriteLine("Demo use of is");

            foreach (Parent element in myArray)
            {
                if (element is GrandChild)
                {
                    Console.WriteLine("GrandChhild");
                }
                else if (element is Child)
                {
                    Console.WriteLine("Child");
                }
                else if (element is Parent)
                {
                    Console.WriteLine("Parent");
                }
            }
            Console.WriteLine();
            #endregion

            #region switch
            Console.WriteLine("Demo switch");
            foreach (Parent element in myArray)
            {
                switch (element)
                {
                case GrandChild gc:
                    Console.WriteLine("GrandChild");
                    break;

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

                case Parent p:
                    Console.WriteLine("Parent");
                    break;

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

            #region object of object type initialized to base class type
            object obj = new Parent();
            ((Parent)obj).ParentMethod();

            Parent p2 = new GrandChild();
            ((GrandChild)p2).SpecializedMethod();
            #endregion
        }
コード例 #5
0
        static void Main(string[] args)
        {
            #region An array of the base class type is created and initialized
            // An array of the base class type is created and initialized with
            // a member of each class in the hierarchy

            Parent[] myArray = new Parent[3];
            myArray[0] = new Parent();
            myArray[1] = new Child();
            myArray[2] = new GrandChild();

            // 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 demostrate that all child classes
            // have access to it

            foreach (Parent element in myArray)
            {
                Console.Write("{0}: ", element.GetType());
                element.ParentMethod();
            }
            Console.WriteLine();
            #endregion

            #region Demonstrate overriding a base class method by a child

            foreach (Parent element in myArray)
            {
                Console.Write("{0}: ", element.GetType());
                element.SomeMethod();
            }
            Console.WriteLine();
            #endregion

            #region Demonstrate a child method calling the parent's method
            // create a virtual method kn parent
            // override in child and call the parent as part of the
            // implementation

            foreach (Parent element in myArray)
            {
                Console.Write("{0}: ", element.GetType());
                element.Method2();
                Console.WriteLine();
            }
            Console.WriteLine();
            #endregion

            #region Demonstrate use of "as" keyword
            // repeat calls to SomeMethod
            foreach (object element in myArray)
            {
                // casting
                Parent p = element as Parent;

                // explict casting
                // Parent p = (Parent)element;

                Console.Write("{0}: ", p.GetType());
                p.SomeMethod();
            }
            #endregion

            #region Demonstrate use of "is" keyword
            Console.WriteLine("Demo use of is keyword");
            foreach (Parent element in myArray)
            {
                // if (element is GrandChild)  Console.WriteLine("GrandChild";

                if (element is GrandChild)
                {
                    Console.WriteLine("GrandChild");
                }
                else if (element is Child)
                {
                    Console.WriteLine("Child");
                }
                else if (element is Parent)
                {
                    Console.WriteLine("Parent");
                }
                Console.WriteLine();
            }
            #endregion
            Console.WriteLine();
            // Demo switch
            #region switch

            foreach (Parent element in myArray)
            {
                switch (element)
                {
                case GrandChild gc:
                    Console.WriteLine("GrandChild");
                    break;

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

                case Parent p:
                    Console.WriteLine("Parent");
                    break;

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

            #region Create an object of "object" type, but initialized of
            //the base class type, then cast it to the actual type so you
            //can call a method;
            {
                object obj = new Parent();

                ((Parent)obj).ParentMethod();

                Parent p2 = new GrandChild();
                ((GrandChild)p2).SpecializedMethod();
            }

            #endregion
        }