static void Main() { //协变,子类传递给父类,存在安全的隐式转换 //逆变,父类传递给子类 //一个可变性和子类到父类转换的方向一样,就称作协变;而如果和子类到父类的转换方向相反,就叫抗变! IIndex<Rectangle> rectangles = RectangleCollection.GetRectangles(); //协变,把Rectangle assign to Shape IIndex<Shape> shapes = rectangles; for (int i = 0; i < shapes.Count; i++) { Console.WriteLine(shapes[i]); } IDisplay<Shape> shapeDisplay = new ShapeDisplay(); shapeDisplay.Show(rectangles[0]); IDisplay<Rectangle> rectangleDisplay = shapeDisplay; rectangleDisplay.Show(rectangles[0]); Console.ReadLine(); }
static void Main() { Shape shape = new Rectangle(200, 120, Color.Blue); // Shape shape = new Triangle(180, 250, Color.Orange); ShapeDisplay shapeDisplay = new ShapeDisplay(shape); Application.Run(shapeDisplay); }
private static void 泛型接口的抗变() { IDisplay <Shape> shapeDisplay = new ShapeDisplay(); IDisplay <Rectangle> rectangleDisplay = shapeDisplay; IIndex <Rectangle> rectangles = RectangleCollection.GetRectangles(); shapeDisplay.Show(rectangles[0]); rectangleDisplay.Show(rectangles[0]); }
public override void Draw(ShapeDisplay sd, Graphics canvas) { base.Draw(sd, canvas); int xStart = sd.Xoffset; int yStart = sd.Yoffset + Height; canvas.DrawLine(pen, xStart, yStart, xStart + Basis, yStart); canvas.DrawLine(pen, xStart, yStart, xStart + Basis / 2, yStart - Height); canvas.DrawLine(pen, xStart + Basis, yStart, xStart + Basis / 2, yStart - Height); }
public override void Draw(ShapeDisplay sd, Graphics canvas) { base.Draw(sd, canvas); // determine position for drawing int xStart = sd.Xoffset; int yStart = sd.Yoffset + this.Height; canvas.DrawLine(pen, xStart, yStart, xStart, yStart - Height); canvas.DrawLine(pen, xStart, yStart, xStart + Width, yStart); canvas.DrawLine(pen, xStart, yStart - Height, xStart + Width, yStart - Height); canvas.DrawLine(pen, xStart + Width, yStart, xStart + Width, yStart - Height); }
static void Main() { IIndex<Rectangle> rectangles = RectangleCollection.GetRectangles(); IIndex<Shape> shapes = rectangles; 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.ReadKey(); }
static void Main(string[] args) { Rectangle[] Rectangles = new Rectangle[3] { new Rectangle { Heigh=2, Width=5}, new Rectangle { Heigh=2, Width=5}, new Rectangle { Heigh=2, Width=5} }; //Class1.Example(); //Console.WriteLine("User-Perferences:BackColor is:" + UserPreferences.BackColor.ToString() + ""); //Console.ReadLine(); IDisplay<Shape> shapeDisplay = new ShapeDisplay(); IDisplay<Rectangle> rectangleDisplay = shapeDisplay; rectangleDisplay.Show(Rectangles[0]); }
public void TestRectangleCollection() { // 协变 IIndex <Rectangle> rectangles = RectangleCollection.GetRectangles(); IIndex <Shape> shapes = rectangles; for (int i = 0; i < shapes.Count; i++) { Debug.WriteLine(shapes[i]); } // 抗变 IDisplay <Shape> shapeDisplay = new ShapeDisplay(); IDisplay <Rectangle> rectangleDisplay = shapeDisplay; Debug.WriteLine(rectangleDisplay.Show(rectangles[0])); }
static void Main() { IIndex<Rectangle> rectangles = RectangleCollection.GetRectangles(); IIndex<Shape> shapes = rectangles; for (int i = 0; i < shapes.Count; i++) { Console.WriteLine(shapes[i]); } IDisplay<Shape> shapeDisplay = new ShapeDisplay(); IDisplay<Rectangle> rectangleDisplay = shapeDisplay; /*Because IDisplay<T> is contra-variant, it is possible to assign the result to IDisplay<Rectangle>, where Rectangle derives from Shape. This time the methods of the interface define only the generic type as input*/ rectangleDisplay.Show(rectangles[0]); }
public virtual void Draw(ShapeDisplay sd, Graphics canvas) { // we define a uniform pen size for all shapes pen = new Pen(this.edgeColor, 3); }
public override void Draw1(ShapeDisplay sd, Graphics canvas) { foreach (var item in) }
public virtual void Draw1(ShapeDisplay sd, Graphics canvas) { pen = new Pen(this.randkleur, 3); }