예제 #1
0
파일: Program.cs 프로젝트: liuqun/csharpdev
        static void Main(String[] args)
        {
            // 协变
            IIndex <Rectangle> rectangles = GenericsRectangle.GetRectangle();
            IIndex <Shape>     shapes     = rectangles;

            // 使用接口中的索引器和Count属性
            for (int i = 0; i < shapes.count; i++)
            {
                Console.WriteLine(shapes[i]);
            }

            // 抗变
            IDisplay <Shape>     shapeDisplay     = new ShapeDisplay();
            IDisplay <Rectangle> rectangleDisplay = shapeDisplay;

            rectangleDisplay.Show(rectangles[0]);

            Console.Read();
        }
예제 #2
0
파일: Program.cs 프로젝트: liuqun/csharpdev
 public static GenericsRectangle GetRectangle()
 {
     // ??为合并运算符:若gr为null将调用运算符右侧以创
     // 建一个GenericsRectangle实例,并将其赋给变量gr
     return(gr ?? (gr = new GenericsRectangle()));
 }