C#委托与协变逆变
协变:子类向父类或基类转化public class A { }
public class B : A { }
public class C : B { }
class Program
{
public static A T1() { return new A(); }
public static B T2(){ return new B();}
public static C T3() { return new C(); }
public delegate object testDeleg();
//public delegate A testDeleg();
static void Main(string[] args)
{
testDeleg testDeleg = T1;
testDeleg testDeleg1 = T2;
testDeleg testDeleg2 = T3;
}
}逆变:父类向子类转化
public class A { }
public class B : A { }
public class C : B { }
class Program
{
public static void T1(A a) { }
public static void T2(B b) { }
public static void T3(C c) { }
public delegate void TestDelegate(C c);
public static TestDelegate dele;
static void Main(string[] args)
{
dele += T1;
dele += T2;
dele += T3;
}
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]