e媒网络

一切皆可能 e媒网络 http://www.eMay.net

博客园 首页 新随笔 联系 订阅 管理

协变参考代码1:

using System;
using System.Collections.Generic;
using System.Linq;

namespace CovarianceSoOnDemo2
{
    public class Pet
    {
        protected bool _bPutOn;
        protected string _name;
        public Pet(string name)
        {
            _name = name;
        }
        public void PutOn()
        {
            _bPutOn = true;         
        }
        public void ShowMe()
        {
            string strPutOn = _bPutOn ? "穿衣的" : "";
            Console.WriteLine(_name + ":"+strPutOn+"小可爱");
        }
    }
    public class Cat : Pet
    {

        public Cat(string name) : base(name)
        {

        }
        public void CatchMouse()
        {
            Console.WriteLine(_name + ":我会抓老鼠");
        }
    }

    public interface IIndex<out T>
    {
        T this[int index] { get; }
        int Count { get; }
    }

    public class CatCollection : IIndex<Cat>
    {
        private Cat[] _data;
        public CatCollection(Cat[] data)
        {
            _data = data;
        }
        public Cat this[int index]
        {
            get
            {
                if (index < 0 || index > _data.Length)
                    throw new ArgumentOutOfRangeException("index");
                return _data[index];
            }
        }        
        public int Count
        {
            get
            {
                return _data.Length;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Cat[] catArr = new Cat[3] {
                new Cat("傻猫1"),
                new Cat("傻猫2"),
                new Cat("傻猫3")
            };
            IIndex<Cat> cats = new CatCollection(catArr);
            IIndex<Pet> pets = cats; 
           for(int i=0;i<pets.Count;i++)
            {
                pets[i].ShowMe();
            }
            for (int i = 0; i < pets.Count; i++)
            {
                pets[i].PutOn();
            }
            for (int i = 0; i < pets.Count; i++)
            {
                pets[i].ShowMe();
                ((Cat)pets[i]).CatchMouse();
            }
        }
    }
}

 协变参考代码2:

using System;

namespace CovarianceSoOnDemo
{
    public class Shape
    {
        public double Width { set; get; }
        public double Height { set; get; }
        public override string ToString()
        {
            return string.Format("Width:{0},Height:{1}", Width, Height);
        }
    }
    public class Rectangle : Shape { }
    public interface IIndex<out T>
    {
        T this[int index] { get; }
        int Count { get; }
    }
    public class RectangleCollection : IIndex<Rectangle>
    {
        private Rectangle[] data = new Rectangle[3]
        {
            new Rectangle{Height=2,Width=5},
            new Rectangle{Height=3,Width=7},
            new Rectangle{Height=4,Width=2.9}
        };
        public Rectangle this[int index]
        {
            get
            {
                if (index < 0 || index > data.Length)
                    throw new ArgumentOutOfRangeException("index");
                return data[index];
            }
        }
        public static  RectangleCollection GetRectangles()
        {
            return new RectangleCollection();
        }
        public int Count
        {
            get
            {
                return data.Length;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            IIndex<Rectangle> rectangles = RectangleCollection.GetRectangles();
            //IIndex<Rectangle> rectangles = new RectangleCollection();
            IIndex<Shape> shapes = rectangles;
            for (int i = 0; i < shapes.Count; i++)
            {
                Console.WriteLine(shapes[i]);
            }
        }
    }
}

 

posted on 2022-12-15 12:54  e媒网络技术团队  阅读(25)  评论(0编辑  收藏  举报