C#中的协变OUT和逆变
泛型接口和泛型委托中经常使用可变性
in 逆变,out 协变
从 list<string>转到list<object> 称为协变 (string 从object 派生,那么 string 转成object 是合理的,子类替换父类是合理的,)
从list<object> 转到 list<string> 称为逆变 (经object 转成string ,将父类转成子类,是不合理的,称为逆变)
1.逆变代码掩饰
static void Main(string[] args)
{
ICovariant<Object> iobj = new Sample<Object>();
ICovariant<String> istr = new Sample<String>(); // You can assign istr to iobj because
// the ICovariant interface is covariant.
// iobj = istr;
istr = iobj; //从 object 类型转成string 类型,父类转成子类 则是逆变
Console.ReadLine();
} // Covariant interface.
interface ICovariant<in R> { } // Extending covariant interface.
interface IExtCovariant<in R> : ICovariant<R> { } // Implementing covariant interface.
class Sample<R> : ICovariant<R> { }
2.协变演示
static void Main(string[] args)
{
ICovariant<Object> iobj = new Sample<Object>();
ICovariant<String> istr = new Sample<String>(); // You can assign istr to iobj because
// the ICovariant interface is covariant.
// iobj = istr;
iobj = istr; //从 string类型转成object类型,子类型转成父类 (里氏替换原则) 则是协变
Console.ReadLine();
} // Covariant interface.
interface ICovariant<out R> { } // Extending covariant interface.
interface IExtCovariant<out R> : ICovariant<R> { } // Implementing covariant interface.
class Sample<R> : ICovariant<R> { }
C#中的协变OUT和逆变的更多相关文章
- C#中的协变(Covariance)和逆变(Contravariance)
摘要 ● 协变和逆变的定义是什么?给我们带来了什么便利?如何应用? ● 对于可变的泛型接口,为什么要区分成协变的和逆变的两种?只要一种不是更方便吗? ● 为什么还有不可变的泛型接口,为什么有的泛型接口 ...
- 协变(covariant)和逆变(contravariant)
我们知道子类转换到父类,在C#中是能够隐式转换的.这种子类到父类的转换就是协变. 而另外一种类似于父类转向子类的变换,可以简单的理解为“逆变”. 上面对逆变的简单理解有些牵强,因为协变和逆变只能针对接 ...
- C# 协变out 、逆变 in
需求:泛型使用多态性 备注:协变逆变只能修饰 接口和委托 简单理解: 1.使用 in 修饰后为逆变,只能用作形参使用 ,参考 public delegate void Action<in T&g ...
- Java中的逆变与协变
看下面一段代码 Number num = new Integer(1); ArrayList<Number> list = new ArrayList<Integer>(); ...
- .NET 4.0中的泛型的协变和逆变
转自:http://www.cnblogs.com/jingzhongliumei/archive/2012/07/02/2573149.html 先做点准备工作,定义两个类:Animal类和其子类D ...
- 转载.NET 4.0中的泛型的协变和逆变
先做点准备工作,定义两个类:Animal类和其子类Dog类,一个泛型接口IMyInterface<T>, 他们的定义如下: public class Animal { } public ...
- Java中的逆变与协变(转)
看下面一段代码 Number num = new Integer(1); ArrayList<Number> list = new ArrayList<Integer>(); ...
- Scala中的协变,逆变,上界,下界等
Scala中的协变,逆变,上界,下界等 目录 [−] Java中的协变和逆变 Scala的协变 Scala的逆变 下界lower bounds 上界upper bounds 综合协变,逆变,上界,下界 ...
- C#中泛型方法与泛型接口 C#泛型接口 List<IAll> arssr = new List<IAll>(); interface IPerson<T> c# List<接口>小技巧 泛型接口协变逆变的几个问题
http://blog.csdn.net/aladdinty/article/details/3486532 using System; using System.Collections.Generi ...
随机推荐
- android 5.0新特性学习--RecyclerView
在过去很多年,我们的PC或者手机设备都是采用拟物化的设计风格,IOS采用扁平化的特性,android在2014年IO大会上说采用Material Design的设计风格,显示效果不能过于生硬的转换,而 ...
- Mysql笔记4数据表操作1
1查看表的结构 (1)show create table +数据库名称 (2)desc+数据库名称 2修改表 (1)表中添加列 alter table 数据库名称 add column addr va ...
- Telepro工具注册码
Teleport Pro v1.54 注册码 Teleport Pro v1.54姓名(Name):3ddown.com序列号(Serial):161594593
- 仿360新闻的热搜图片,win8风格随机九宫格布局
360新闻地址:http://sh.qihoo.com/i/ 感觉这效果挺好的,随机九宫格,在不少地方可以用到,就研究了下他的源码,基本原理就是预先定义好几种布局模块,然后根据需要进行拼接,具体代码可 ...
- 这丫头也的还真清楚,但是跑不通呢,换3.0.3的mybatis也不行
http://java.dzone.com/articles/ibatis-mybatis-handling-joins http://mybatis.github.io/spring/mappers ...
- 老问题:Android子线程中更新UI的3种方法
在Android项目中经常有碰到这样的问题,在子线程中完成耗时操作之后要更新UI,下面就自己经历的一些项目总结一下更新的方法: 方法一:用Handler 1.主线程中定义Handler: Handle ...
- inotify-java linux系统监听文件发生变化,实时通知java程序
1 Overview 最近公司的一个任务需要实时监控文件系统中某个文件的内容变化.由于程序本身由Java编写,因此使用了inotify- java(http://code.google.com ...
- 深入浅出Ajax(二)
<script type="text/javascript"> window.onload = initPage; function initPage() { var ...
- webapp之路--百度手机前端经验(转)
看了之后收获很大,分享一下: 本文将围绕我半年来在移动前端工程化做的一些工作做的总结,主要从localstorage缓存和版本号管理,模块化,静态资源渲染方式三个方面总结手机百度前端一年内沉淀的解决方 ...
- python的一些语法糖
1 Python中if-else语句的多种写法 a, b, c = 1, 2, 3 1.常规 if a>b: c = a else: c = b 2.表达式 c = a if a>b ...