コード例 #1
0
ファイル: Program.cs プロジェクト: arigatory/lec3
        static void Main(string[] args)
        {
            Square square = new Square();

            square.Draw();

            Circle circle = new Circle();

            circle.Draw();

            Shape[] shapes = new Shape[]
            {
                new Circle(), new Square(), new Rectange(), new Point(),
            };

            for (int i = 0; i < shapes.Length; i++)
            {
                shapes[i].Draw();
                shapes[i].GetArea();
            }

            Point p = new Point();

            if (p is Rectange)
            {
                Rectange r = (Rectange)p;
                r.Draw();
                Console.WriteLine("True");
            }

            Console.ReadLine();
        }
コード例 #2
0
            static void Main(string[] args)
            {
                Rectange rectange = new Rectange();

                rectange.setWidth(5);
                rectange.setHeigh(7);
                // Print the area of the object.
                Console.WriteLine("Total area: {0}", rectange.getArea());
            }
コード例 #3
0
        static void Fill(int x1, int y1, int x2, int y2)
        {
            if ((x1 >= x2) || (y1 >= y2))
            {
                return;
            }

            Rectange rect = new Rectange(x1, y1, x2, y2);

            rect.Draw();
            Fill(x1 + 1, y1 + 1, x2 - 1, y2 - 1);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: heartlove/test1
        static void Main(string[] args)
        {
            ICovariant <Sharp>    isharp = new Sharp();
            ICovariant <Rectange> irect  = new Rectange();

            isharp = irect;
            //irect = isharp;

            Sharp testShape = isharp.Method1();

            int?testNull = null;

            testNull = 6;

            string strTest = "aaaaaa aaaaa";

            strTest.Split(' ');
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: ChegnduJackli/Projects
        static void Main(string[] args)
        {
            /*
            ICovariant<Sharp> isharp = new Sharp();
            ICovariant<Rectange> irect = new Rectange();

            //要使下面的语句成立,需要将接口声明为Out
            //如果一个泛型接口IFoo<T>,IFoo<TSub>可以转换为IFoo<TParent>的话,我们称这个过程为协变,而且说“这个泛型接口支持对T的协变”。
            isharp = irect;
            */

            //那我如果反过来呢,考虑如下代码:
            ICovariant<Sharp> isharp = new Sharp();
            ICovariant<Rectange> irect = new Rectange();
            //要使下面的语句成立,需要将接口声明为In
            //如果一个泛型接口IFoo<T>,IFoo<TParent>可以转换为IFoo<TSub>的话,我们称这个过程为抗变(contravariant),而且说“这个泛型接口支持对T的抗变”!
            irect = isharp;
            Console.ReadLine();
        }
コード例 #6
0
        }//End of Main

        //Based on variable and return 'Shape' variable
        public static Shape GetObject(int flag)
        {
            Shape shObj;

            if (flag == 1) //Let say '1' is for Squar
            {
                shObj = new Square();
            }
            else if (flag == 2) //Let say '2' is for Squar
            {
                shObj = new Rectange();
            }
            else if (flag == 3) //Let say '3' is for Triange
            {
                shObj = new Triangle();
            }
            else
            {
                shObj = null;
            }

            return(shObj);
        }
コード例 #7
0
        static void Main(string[] args)
        {
            //----------set some value to abstract class static variables
            Shape.counter = 5;

            //----------Following statement is not allowed, we can't create Object of abstract class
            //Shape shObj = new Shape();

            /*----------We can create 'Reference Variable' of abstract class*/
            Shape shObj = null;

            /*---------Create object of child class and assign it to parent class 'Reference variable' */
            Console.WriteLine("--------------Reference Var:Shape,  Object:Square-------------------------");
            shObj = new Square();
            shObj.Show();
            shObj.Draw();
            shObj.HideMe();

            //Call method which takes any object of type 'Shape'
            DoSomething(shObj);

            Console.WriteLine("-----------------End-------------------------------------");

            /*---------Create object of child class and assign it to parent class 'Reference variable' */
            Console.WriteLine("--------------Reference Var:Shape,  Object:Rectange-------------------------");
            shObj = new Rectange();
            shObj.Show();
            shObj.Draw();
            shObj.HideMe();

            //Call method which takes any object of type 'Shape'
            DoSomething(shObj);

            Console.WriteLine("-----------------End-------------------------------------");

            /*---------Create object of child class and assign it to parent class 'Reference variable' */
            Console.WriteLine("--------------Reference Var:Shape,  Object:Triangle-------------------------");
            shObj = new Triangle();
            shObj.Show();
            shObj.Draw();
            shObj.HideMe();

            //Call method which takes any object of type 'Shape'
            DoSomething(shObj);

            Console.WriteLine("--------------Type Casting..Reference Var:Triangle,  Object:Triangle-------------------------");

            //As shObj refering to 'Trainage' object, we can type cast to it 'Triangle'
            Triangle triangObj = (Triangle)shObj;

            triangObj.Show();
            triangObj.Draw();
            triangObj.HideMe();

            //Call method which takes any object of type 'Shape'
            DoSomething(triangObj);

            Console.WriteLine("-----------------End-------------------------------------");


            /*---------Create object of child class and assign it to child class 'Reference variable' */
            Console.WriteLine("--------------Reference Var:Square,  Object:Square-------------------------");
            Square sqObj = new Square();

            sqObj.Show();
            sqObj.Draw();
            sqObj.HideMe();

            //Call method which takes any object of type 'Shape'
            DoSomething(sqObj);

            Console.WriteLine("-----------------End-------------------------------------");

            /*---------Create object of child class and assign it to child class 'Reference variable' */
            Console.WriteLine("--------------Reference Var:Rectange,  Object:Rectange-------------------------");
            Rectange rqObj = new Rectange();

            rqObj.Show();
            rqObj.Draw();
            rqObj.HideMe();

            //Call method which takes any object of type 'Shape'
            DoSomething(rqObj);

            Console.WriteLine("-----------------End-------------------------------------");


            /*---------Create object of child class and assign it to child class 'Reference variable' */
            Console.WriteLine("--------------Reference Var:Triangle,  Object:Triangle-------------------------");
            Triangle trObj = new Triangle();

            trObj.Show();
            trObj.Draw();
            trObj.HideMe();

            //Call method which takes any object of type 'Shape'
            DoSomething(trObj);

            Console.WriteLine("-----------------End-------------------------------------");

            Console.WriteLine("---------------Custom Object creation----------------------------");

            int    type   = 0;
            string choice = "";

            do
            {
                Console.WriteLine("Enter 1 to Create 'Square'");
                Console.WriteLine("Enter 2 to Create 'Rectangle'");
                Console.WriteLine("Enter 3 to Create 'Triangle'");

                //Get a number from user
                type = Convert.ToInt32(System.Console.ReadLine());

                //Function will return object of type 'Shape' which would be refering to 'Square' or 'Rectange' or 'Trinage'
                Shape sObj = GetObject(type);

                //Call 'Draw' method. It will be decided at run time based on the available object in 'sObj'
                sObj.Draw();
                sObj.Show();

                Console.Write("Do you want to create again? (Y/N):");
                choice = System.Console.ReadLine();
            }while (choice == "Y" || choice == "y");


            Console.WriteLine("-----------------End-------------------------------------");


            /* 'Object' is base class of every class eventually
             * so it can hold reference of object of any calls */

            Object o1 = new Object();
            Object o2 = new Triangle();
            Object o3 = new Square();
            Object o4 = new Rectange();


            //To stop the console so user presses a key to exit application
            System.Console.ReadKey();
        }//End of Main