コード例 #1
0
ファイル: CCTest.cs プロジェクト: junchenglai/csharp
        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>(); // error: 语法错误:List<Bird>是一个类,List<Sparrow>也是一个类,两者没有父子关系,List<> 之间没有关系。

                List <Bird> birdList3 = new List <Sparrow>().Select(c => (Bird)c).ToList();
            }
            {
                // 协变:就是可以让右边用上子类
                // out 修饰后,T 只能作为返回值,不能当参数
                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>();
                customerList2.Get();
            }

            {
                // 逆变:就是可以让右边用上父类
                // in 修饰后,T 只能作为参数,不能当返回值
                ICustomerListIn <Sparrow> customerList1 = new CustomerListIn <Bird>();
                ICustomerListIn <Sparrow> customerList2 = new CustomerListIn <Sparrow>();
                customerList1.Show(new Sparrow());

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

                Action <Sparrow> action = 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>();    // 协变 + 逆变
            }
        }
コード例 #2
0
        public static void Show()
        {
            {
                Bird    b1 = new Bird();
                Bird    b2 = new Sparrow();
                Sparrow b3 = new Sparrow();
                // Sparrow b4 = new Bird();
            }
            {
                List <Bird> birds = new List <Bird>();
                // List<Bird>birds1 = new List<Sparrow>();

                List <Bird> birds2 = new List <Sparrow>()
                                     .Select(c => ((Bird)c)).ToList();
            }
            {
                // 协变
                IEnumerable <Bird> birds  = new List <Bird>();
                IEnumerable <Bird> birds1 = 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) => { });
            }
        }