协变:让返回值类型返回比声明的类型派生程度更大的类型,就是“协变”。
class Program { static void Main(string[] args) { ISalary<Programmer> s = new BaseSalaryCounter<Programmer>(); PrintSalary(s); } static void PrintSalary<T>(ISalary<T> s) { s.Pay(); } } interface ISalary<T> { void Pay(); } class BaseSalaryCounter<T> : ISalary<T> { public void Pay() { Console.WriteLine("Pay Base Salary!"); } } class Employee { public string Name { get; set; } } class Programmer : Employee { } class Manager : Employee { }
标签:Pay,void,42,Employee,ISalary,泛型,可变性,class From: https://www.cnblogs.com/wen-chen/p/16994367.html