public static void Show()
        {
            {
                Bird    bird1    = new Bird();
                Bird    bird2    = new Sparrow();
                Sparrow sparrow1 = new Sparrow();
                // Sparrow sparrow2 = new Bird();
            }
            {
                List <Bird> bird1 = new List <Bird>();

                // 应该可以这样写, 一堆麻雀当然是一堆鸟
                // 但是实际上不行。
                // 因为 List<Bird> 和 List<Sparrow>
                // 没有父子关系。
                // List<Bird> bird2 = new List<Sparrow>();



                List <Bird> birds3 = new List <Sparrow>().Select(c => (Bird)c).ToList();
            }

            {
                // 协变

                // 之所以可以这样写,是因为在泛型接口IEnumerable当中
                // 在泛型类型前使用了out
                // out 修饰后,泛型类型只能放在返回值而不能放在函数入参中
                IEnumerable <Bird> birds1 = new List <Bird>();
                IEnumerable <Bird> birds2 = new List <Sparrow>();

                //委托
                Func <Bird> func = new Func <Sparrow>(() => null);

                ICustomerListOut <Bird> customerListOut  = new CustomerListOut <Bird>();
                ICustomerListOut <Bird> customerListOut2 = new CustomerListOut <Sparrow>();
            }

            {
                // 逆变
                ICustomerListIn <Sparrow> customerListIn1 = new CustomerListIn <Sparrow>();
                ICustomerListIn <Sparrow> customerListIn2 = new CustomerListIn <Bird>();

                ICustomerListIn <Bird> birds = new CustomerListIn <Bird>();
                birds.Show(new Sparrow());
                birds.Show(new Bird());

                Action <Sparrow> act = new Action <Bird>((Bird i) => {});
            }

            {
                IMyList <Sparrow, Bird> myList1 = new MyList <Sparrow, Bird>();
                IMyList <Sparrow, Bird> myList2 = new MyList <Sparrow, Sparrow>(); //协变
                IMyList <Sparrow, Bird> myList3 = new MyList <Bird, Bird>();       // 逆变
                IMyList <Sparrow, Bird> myList4 = new MyList <Bird, Sparrow>();    // 逆变 + 协变
            }
        }
Exemple #2
0
        public static void Show()
        {
            {
                Bird    bird1    = new Bird();
                Bird    bird2    = new Sparrow();
                Sparrow sparrow1 = new Sparrow();
                /* Sparrow sparrow2 = new Bird()*/
                ;
            }

            {
                List <Bird> birdList1 = new List <Bird>();
                //List<Bird> birdList2 = new List<Sparrow>();
                //难道一组麻雀不是一组鸟吗?
                //List<Bird> 是一个类, List<Sparrow> 是另外一个类,二者没有继承

                List <Bird> birdList3 = new List <Sparrow>().Select(c => (Bird)c).ToList();
            }
            //泛型在使用的时候,会存在不和谐的地方
            // 协变和逆变  都是在泛型中才有,在泛型接口 或者泛型委托才有
            {//out修改类型参数,协变:可以让右边使用子类
             // 使用协变时候,可以把子类放在右边,这才是泛型该有的样子

                IEnumerable <Bird> birdList1 = new List <Bird>();
                IEnumerable <Bird> birdList2 = new List <Sparrow>(); //为什么可以写?

                ICustomerListOut <Bird> customerList1 = new CustomerListOut <Bird>();
                ICustomerListOut <Bird> customerList2 = new CustomerListOut <Sparrow>();
            }

            {//逆变
             //int修改类型参数,逆变:可以让右边使用父类
             // 类型参数只能作为参数 ,不能作为返回值

                ICustomerListIn <Sparrow> customerList2 = new CustomerListIn <Sparrow>();
                ICustomerListIn <Sparrow> customerList1 = new CustomerListIn <Bird>();

                customerList1.Show(new Sparrow());

                ICustomerListIn <Bird> birdList1 = new CustomerListIn <Bird>();
                birdList1.Show(new Sparrow());
                birdList1.Show(new Bird());
            }

            {
                IMyList <Sparrow, Bird> myList1 = new MyList <Sparrow, Bird>();
                IMyList <Sparrow, Bird> myList2 = new MyList <Sparrow, Sparrow>(); //协变
                IMyList <Sparrow, Bird> myList3 = new MyList <Bird, Bird>();       //逆变
                IMyList <Sparrow, Bird> myList4 = new MyList <Bird, Sparrow>();    //协变+逆变
            }

            //完全能理清楚 刷个1
            //能清楚80%  2   60%  3
        }
Exemple #3
0
        public static void Show()
        {
            {
                Bird    bird1    = new Bird();
                Bird    bird2    = new Sparrow();
                Sparrow sparrow1 = new Sparrow();
                //Sparrow sparrow2 = new Bird();
            }

            {
                List <Bird> birdList1 = new List <Bird>();
                //List<Bird> birdList2 = new List<Sparrow>();
                //应该可以呀   一堆麻雀当然是一堆鸟
                //没有父子关系

                List <Bird> birdList3 = new List <Sparrow>().Select(c => (Bird)c).ToList();
            }
            {//协变
                IEnumerable <Bird> birdList1 = new List <Bird>();
                IEnumerable <Bird> birdList2 = new List <Sparrow>();

                Func <Bird> func = new Func <Sparrow>(() => null);

                ICustomerListOut <Bird> customerList1 = new CustomerListOut <Bird>();
                ICustomerListOut <Bird> customerList2 = new CustomerListOut <Sparrow>();
            }



            {//逆变
                ICustomerListIn <Sparrow> customerList2 = new CustomerListIn <Sparrow>();
                ICustomerListIn <Sparrow> customerList1 = new CustomerListIn <Bird>();

                ICustomerListIn <Bird> birdList1 = new CustomerListIn <Bird>();
                birdList1.Show(new Sparrow());
                birdList1.Show(new Bird());

                Action <Sparrow> act = new Action <Bird>((Bird i) => { });
            }


            {
                IMyList <Sparrow, Bird> myList1 = new MyList <Sparrow, Bird>();
                IMyList <Sparrow, Bird> myList2 = new MyList <Sparrow, Sparrow>(); //协变
                IMyList <Sparrow, Bird> myList3 = new MyList <Bird, Bird>();       //逆变
                IMyList <Sparrow, Bird> myList4 = new MyList <Bird, Sparrow>();    //逆变+协变
            }
        }