[英]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.