我有以下类层次结构
public class EntityBase<T> where T : EntityBase<T>
{
//nothing interesting here
}
public class Benefit : EntityBase<Benefit>
{
//again, nothing interesting here
}
public class SeasonTicketLoan : Benefit
{
//nothing interesting here
}
现在我有以下界面
public interface IQuery<T> where T : EntityBase<T>
{
}
当我尝试构建以下类时,出现编译错误
public class EmployeesQuery : IQuery<SeasonTicketLoan>
{
}
我收到一条错误消息SeasonTicketLoan
类不满足约束。
The Benefit
类也应该有一个泛型类型 - 所以所有父类都有“最终的”/sealed 类型作为它们的通用类型。只有“终极”/密封类型没有通用参数。
结果是,在所有父类中一直到根父类,泛型参数都包含“最终”/密封类的类型,并且不会出现错误。
public class EntityBase<T> where T : EntityBase<T>
{
//nothing interesting here
}
public class Benefit<T> : EntityBase<T> where T : Benefit<T>
{
//again, nothing interesting here
}
public sealed class SeasonTicketLoan : Benefit<SeasonTicketLoan>
{
//nothing interesting here
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)