public class Fa : TranOut<Fa>, TranIn<Fa> { } public class son : Fa, TranOut<son>, TranIn<son> { } public interface TranOut<out t> { } public interface TranIn<in t> { } public class Test { public void test() { //c#4.0 IEnumerable<Fa> sharps = new List<son>(); Fa a = new son(); TranOut<Fa> b = new son(); TranIn<son> c = new Fa(); IEnumerable<Fa> la = new List<son>(); } }
用out作为协变关键字,in为抗变关键字
使用out可以将子类转成父类对象,in则反之