Exemple #1
0
        static void Main(string[] args)
        {
            var        square        = new Square(10);
            var        adapter       = new SquareToRectangleAdapter(square);
            IRectangle adaptedSquare = adapter;

            Console.WriteLine(adaptedSquare.Area());
        }
Exemple #2
0
        public static void Main()
        {
            Shape s = new Shape();

            // s.Area(10, 10); // error - ambiguous !!!
            // s.IRectangle.Area(10, 10); // error
            // s.ITriangle.Area(10, 10); // error
            ((IRectangle)s).Area(20, 20); // 캐스팅-업
            ((ITriangle)s).Area(20, 20);  // 캐스팅-업
            IRectangle r = s;             // 인터페이스로 캐스팅-업
            ITriangle  t = s;             // 인터페이스로 캐스팅-업

            r.Area(30, 30);
            t.Area(30, 30);
        }
        static void Main(string[] args)
        {
            IRectangle rect1 = ShapeFactory.GetInstance("Rectangle", 3, 4) as IRectangle;

            Console.WriteLine($"The area is {rect1.Area()}");
            IRectangle rect2 = ShapeFactory.GetInstance("Rectangle") as IRectangle;
            // "as" - type cast without exceptions
            // ICircle circ1 = new Circle(1.5);
            // ICircle circ2 = new Circle();
            ISquare sq = ShapeFactory.GetInstance("Square", 3) as ISquare;

            //IRectangle ir = new NewRectangle();

            IShape[] arr = { rect1, rect2, sq };

            arr.ToList().ForEach(Console.WriteLine);
        }