简体   繁体   English

c#从泛型类继承

[英]c# inheriting from generic class

Let me describe the logic and then class structure. 让我先描述逻辑,然后再描述类结构。 There are objects and all object must inherit from ConfigurationObjectBase. 有对象,所有对象都必须继承自ConfigurationObjectBase。 Each object must be owned by Manager and all Managers must be derived from ConfigurationObjectManagerBase. 每个对象必须由Manager拥有,并且所有Manager必须派生自ConfigurationObjectManagerBase。 When a new instance of object created, one of the constructor must accept instance of Manager and that instance of Manager must add that instance of object into it's property called ChildObjects. 创建对象的新实例时,构造函数之一必须接受Manager的实例,并且Manager的实例必须将该对象的实例添加到其名为ChildObjects的属性中。 Below is sample of classes. 以下是课程示例。 could you pls help to correct in below code acording above business rule? 您能否根据业务规则,在下面的代码中帮助更正? Thanks. 谢谢。

public class ConfigurationObjectBase<ObjectType>
{
    public ConfigurationObjectBase(ConfigurationObjectManagerBase<ObjectType> ownerManager)
    {
        ownerManager.ChildObjects.Add(this);
    }
}


public class ConfigurationObjectManagerBase<ObjectType>
{
    public ConfigurationObjectManagerBase()
    {
        ChildObjects = new List<ObjectType>();
    }

    public List<ObjectType> ChildObjects { get; set; }
}

public class Catalog : ConfigurationObjectBase<Catalog>
{
    public Catalog(CatalogManager ownerManager) : base(???)
    {

    }
}

public class CatalogManager : ConfigurationObjectManagerBase<CatalogManager>
{
    public CatalogManager() : base()
    {
    }
}

There are two issues in your code: 您的代码中有两个问题:

  • CatalogManager should inherit from ConfigurationObjectManagerBase<Catalog> , not ConfigurationObjectManagerBase<CatalogManager> CatalogManager应该继承自ConfigurationObjectManagerBase<Catalog> ,而不继承自ConfigurationObjectManagerBase<CatalogManager>
  • ChildObjects should probably be a list of ConfigurationObjectBase<ObjectType> , rather than a list of ObjectType (otherwise you can't add a ConfigurationObjectBase<ObjectType> to it) ChildObjects可能应该是ConfigurationObjectBase<ObjectType>的列表,而不是ObjectType的列表(否则,您不能向其添加ConfigurationObjectBase<ObjectType>

So the code should probably look like this: 因此,代码可能看起来像这样:

public class ConfigurationObjectBase<ObjectType>
{
    public ConfigurationObjectBase(ConfigurationObjectManagerBase<ObjectType> ownerManager)
    {
        ownerManager.ChildObjects.Add(this);
    }
}


public class ConfigurationObjectManagerBase<ObjectType>
{
    public ConfigurationObjectManagerBase()
    {
        ChildObjects = new List<ConfigurationObjectBase<ObjectType>>();
    }

    public List<ConfigurationObjectBase<ObjectType>> ChildObjects { get; set; }
}

public class Catalog : ConfigurationObjectBase<Catalog>
{
    public Catalog(CatalogManager ownerManager) : base(ownerManager)
    {

    }
}

public class CatalogManager : ConfigurationObjectManagerBase<Catalog>
{
    public CatalogManager()
    {
    }
}

Also, you don't need to call the default base class constructor ( base() ), it's done implicitly by the compiler. 另外,您不需要调用默认的基类构造函数( base() ),它是由编译器隐式完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM