与继承的类类型的泛型列表类 [英] Class with generic list of the inherited class type

查看:272
本文介绍了与继承的类类型的泛型列表类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3类:

一些主题有某种类型的列表称为后代。

我想有一个泛型类的BaseHeaderFooterItem类。
和insery和列表的类型吧。

有什么选择呢?

 #区域ParentItem
公共类BaseHeaderFooterItem
{
    公共字符串名称{搞定;组; }
    公共字符串赋予{搞定;组; }
    公共超链接链接{搞定;组; }
    公众诠释级别{搞定;组; }
}
#endregion#区域的HeaderFooter
公共类的HeaderFooter:BaseHeaderFooterItem
{
    公开名单<类别及GT;后人{搞定;组; }
}
#endregion#区域的HeaderFooter
公共类类别:BaseHeaderFooterItem
{
    公开名单<展览及GT;后人{搞定;组; }
}
#endregion#区域标题
公共类显示:的HeaderFooter
{
    公共字符串的ImagePath {搞定;组; }
    公共字符串随身宝{搞定;组; }
    公共字符串MobileLinkTarget {搞定;组; }
}
#endregion#区域TvGuid
公共类TvGuid:展
{
    公共字符串日期{搞定;组; }
    公共字符串时间{获得;组; }
    公众诠释IsActive {搞定;组; }
    公众诠释NoProgram {搞定;组; }
}
#endregion


解决方案

如果你想使用仿制药,你可以使用泛型:

 #区域ParentItem
    公共类BaseHeaderFooterItem< T>
        其中T:类
    {
        公共字符串名称{搞定;组; }
        公共字符串赋予{搞定;组; }
        公共超链接链接{搞定;组; }
        公众诠释级别{搞定;组; }
        公开名单< T>后人{搞定;组; }
    }
    #endregion    #区域的HeaderFooter
    公共类的HeaderFooter:BaseHeaderFooterItem<类别及GT;
    {
    }
    #endregion    #区域的HeaderFooter
    公共类类别:BaseHeaderFooterItem<展览及GT;
    {
    }
    #endregion    #区域标题
    公共类显示:的HeaderFooter
    {
        公共字符串的ImagePath {搞定;组; }
        公共字符串随身宝{搞定;组; }
        公共字符串MobileLinkTarget {搞定;组; }
    }
    #endregion    #区域TvGuid
    公共类TvGuid:展
    {
        公共字符串日期{搞定;组; }
        公共字符串时间{获得;组; }
        公众诠释IsActive {搞定;组; }
        公众诠释NoProgram {搞定;组; }
    }
    #endregion

I have 3 class:

some of theme have list called descendents of some type.

I would like to have a generic class in the BaseHeaderFooterItem class. and insery and type of list to it.

Is there any option ?

#region ParentItem
public class BaseHeaderFooterItem
{
    public string Title { get; set; }
    public string EnTitle { get; set; }
    public HyperLink Link { get; set; }
    public int Level { get; set; }
}
#endregion

#region HeaderFooter
public class HeaderFooter : BaseHeaderFooterItem
{
    public List<Category> Descendants { get; set; }
}
#endregion

#region HeaderFooter
public class Category : BaseHeaderFooterItem
{
    public List<Show> Descendants { get; set; }
}
#endregion

#region Header
public class Show : HeaderFooter
{
    public string ImagePath { get; set; }
    public string MobileLink { get; set; }
    public string MobileLinkTarget { get; set; }
}
#endregion

#region TvGuid
public class TvGuid : Show
{
    public string Date { get; set; }
    public string Time { get; set; }
    public int IsActive { get; set; }
    public int NoProgram { get; set; }
}
#endregion

解决方案

If you want to use generics, you could use generics:

 #region ParentItem
    public class BaseHeaderFooterItem<T>
        where T:class 
    {
        public string Title { get; set; }
        public string EnTitle { get; set; }
        public HyperLink Link { get; set; }
        public int Level { get; set; }
        public List<T> Descendants { get; set; }
    }
    #endregion

    #region HeaderFooter
    public class HeaderFooter : BaseHeaderFooterItem<Category>
    {
    }
    #endregion

    #region HeaderFooter
    public class Category : BaseHeaderFooterItem<Show>
    {
    }
    #endregion

    #region Header
    public class Show : HeaderFooter
    {
        public string ImagePath { get; set; }
        public string MobileLink { get; set; }
        public string MobileLinkTarget { get; set; }
    }
    #endregion

    #region TvGuid
    public class TvGuid : Show
    {
        public string Date { get; set; }
        public string Time { get; set; }
        public int IsActive { get; set; }
        public int NoProgram { get; set; }
    }
    #endregion

这篇关于与继承的类类型的泛型列表类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆