简体   繁体   English

是否可以为不同类型创建通用抽象父类?

[英]Is it possible to create a generic abstract parent class for different types?

I was curious if it was possible to create like a parent abstract class that I can define a specific set of methods in but have the children classes include different entity types? 我很好奇是否可以创建像父抽象类那样可以定义一组特定方法的子类,但子类是否包含不同的实体类型? Code Example: 代码示例:

public abstract class BaseService
{
    public abstract void Add();

    public abstract void Delete();

    public abstract void Update();

    public abstract void Get();        
}

Maybe be able to do something like public abstract List<'random type'> GetAll(); 也许能够执行类似public abstract List<'random type'> GetAll();

But here i would want to override each method with specific parameters that are specific to each of its children: 但是在这里,我想用特定于其每个子级的特定参数覆盖每个方法:

public class CategoryService : BaseService
{
    public override void Add(){ }

    public override void Delete(){ }

    public override void Update(){ }

    public override void Get(){ }
}

However, in my child class, I would want my Get() method to return a specific List<"of Type"> (in this case Category). 但是,在我的子类中,我希望我的Get()方法返回特定的List <“ ofType”>(在本例中为Category)。 Furthermore, I might want to do public override Add(int CategoryID) instead of the inherited Add from BaseService . 此外,我可能想进行public override Add(int CategoryID)而不是从BaseService继承的Add

Is this possible? 这可能吗? Thoughts? 有什么想法吗? Am I just crazy? 我疯了吗? Or am I trying to make this more complicated than it needs to be? 还是我想使它变得比需要的复杂? I have about 10 different service types that I want to make sure get those generic methods from BaseService . 我想确保从BaseService获得那些通用方法约有10种不同的服务类型。

Thanks in advance! 提前致谢!

You could do this if I understand you correctly: 如果我对您的理解正确,则可以执行以下操作:

public abstract class BaseService<T>
{
    public abstract T Get();        
}

Then: 然后:

public class CategoryService : BaseService<Category>
{
    public override Category Get(){ ... }
}

You can't override methods and have a different signature in the override, but you could use a dictionary/hashtable or something more fancy to pass parameters into the Add method. 您不能覆盖方法并且在覆盖中具有不同的签名,但是您可以使用字典/哈希表或其他一些花哨的方法来将参数传递给Add方法。 Passing parameters in using a generic container would mean you are starting to use the query pattern where the parameters in the container determine the query (just for info :-)). 使用通用容器传递参数意味着您将开始使用查询模式,其中容器中的参数确定查询(仅针对info :-)。

Try using generic type 尝试使用通用类型

public abstract List<T> GetAll<T>();

And return appropriate type in your child class 并在您的孩子班级中返回适当的类型

Update 更新资料

@pw94 answer is also a good way to achieve this, but the only problem is you cannot have multiple type for different methods, only one type will work once you inherit the class. @ pw94答案也是实现此目的的一种好方法,但是唯一的问题是您不能为不同的方法使用多个类型,一旦继承了该类,则只有一个类型将起作用。

You can create abstract generic class: 您可以创建抽象的泛型类:

public abstract class BaseService<T>
{
    public abstract void Add();

    public abstract List<T> Get();        
}

And implement: 并实现:

public class CategoryService : BaseService<int>
{
    public override void Add(){ ... }

    public override List<int> Get(){ ... }
}

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

相关问题 如何创建返回不同接口类型和抽象类的通用工厂 - How to create a generic factory returning different interface types and an abstract class 创建从抽象父类继承的不同类型的集合 - Create a collection of different types inherited from abstract parent class C#是否可以将Generic Abstract类的单个参数限制为两个不同的用户定义类型? - C# Is it possible to restrict a Generic Abstract class single parameter to two different user defined types? 抽象类中的泛型类型和方法 - Generic types and method in an abstract class 如何在抽象中使用泛型 class - How to use generic types in abstract class 是否可以在抽象 class 中创建事件? - Is it possible to create an event in an abstract class? 具有不同类型参数的类的抽象类 - Abstract class for classes with different types of parameters Autofac:将开放的通用类型注册为非通用抽象父类型的实例吗? - Autofac: register open generic types as instances of non-generic abstract parent types? 具有返回不同类型子项的抽象类中的C#抽象方法 - C# abstract method in abstract class with children that return different types 是否有一个Collection将实现保存到具有多种类型的抽象泛型类 - Is there a Collection that hold implementations to an abstract generic class with multiple types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM