如何让派生类函数被执行?
我们有一个基类:Filter。 DistrFilter 和 ReportFilter 继承自 Filter。
在另一个类 FilterService.cs 中,我们有两个接受这三种类类型的函数。 FilterService 对 Filter 对象进行操作,但它不继承任何东西。
public class FilterService
{
public string GetDesc(List<T> filters) where T : Filter
{
if(filters.Count == 0) return String.Empty;
StringBuilder s = new StringBuilder("<ul>");
foreach (T f in filters)
s.AppendFormat("<li>{1}</li>", GetFilterText(f));
s.Append("</ul>");
return s.ToString();
}
public string GetFilterText(Filter f)
{
return "filter";
}
public string GetFilterText(DistrFilter f)
{
return "distr filter";
}
public string GetFilterText(ReportFilter f)
{
return "report filter";
}
}
public static void main(string[] args)
{
List<DistrFilter> distrFilters = new List<DistrFilters>();
distrFilters.Add(new DistrFilter());
distrFilters.Add(new DistrFilter());
distrFilters.Add(new DistrFilter());
FilterService fs = new FilterService();
Console.WriteLine(fs.GetDescription(distrFilters));
}
然后打印:
- 过滤器
- 过滤器 ...
我怎样才能让它打印这个呢?
- 分布过滤器
- 分布过滤器 ...
We have a base class: Filter. DistrFilter and ReportFilter inherit from Filter.
In another class, FilterService.cs, we have two functions that accept these three class types. FilterService operates on Filter objects but it does not inherit from anything.
public class FilterService
{
public string GetDesc(List<T> filters) where T : Filter
{
if(filters.Count == 0) return String.Empty;
StringBuilder s = new StringBuilder("<ul>");
foreach (T f in filters)
s.AppendFormat("<li>{1}</li>", GetFilterText(f));
s.Append("</ul>");
return s.ToString();
}
public string GetFilterText(Filter f)
{
return "filter";
}
public string GetFilterText(DistrFilter f)
{
return "distr filter";
}
public string GetFilterText(ReportFilter f)
{
return "report filter";
}
}
public static void main(string[] args)
{
List<DistrFilter> distrFilters = new List<DistrFilters>();
distrFilters.Add(new DistrFilter());
distrFilters.Add(new DistrFilter());
distrFilters.Add(new DistrFilter());
FilterService fs = new FilterService();
Console.WriteLine(fs.GetDescription(distrFilters));
}
Then it prints:
- filter
- filter
...
How do I get it to print this instead?
- distr filter
- distr filter
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
向
Filter
类添加一个虚拟方法,称为GetName()
或类似的方法,并在return "distr filter";
中实现它code>DistrFilter 并在ReportFilter
中作为return "report filter";
。然后只需在GetDesc()
中调用f.GetName()
即可。或者,您可以在
GetDesc()
中使用诸如if (f is DistrFilter)
之类的检查,但这种结构(显式检查特定派生类并以不同方式处理它们)是通常被认为是糟糕的设计。Add a virtual method to the
Filter
class, calledGetName()
or something similar, and implement it asreturn "distr filter";
inDistrFilter
and asreturn "report filter";
inReportFilter
. Then just callf.GetName()
inGetDesc()
.Alternatively, you could use checks like
if (f is DistrFilter)
inGetDesc()
, but that sort of structure, explicitly checking for specific derived classes and handling them differently, is generally considered poor design.要么将
GetFilterText()
实现为Filter
中的虚拟方法:然后执行以下操作:
或者如果您想将指定过滤器文本的问题与
Filter< 分开/code> 类,使用双重调度(访问者模式)。如果您可以拥有不同类型的过滤器服务,这将很有用。可以这样完成:
然后在您的服务中:
一些参考:双重调度,访客模式
Either implement
GetFilterText()
as a virtual method inFilter
:Then do this:
Or if you want to separate the concern of specifying the filter text from the
Filter
classes, use double dispatch (visitor pattern). This is useful if you can have different types of filter services. This can be done this way:Then in your service:
Some references: Double dispatch, Visitor Pattern
实现一个执行如下操作的模式:
Implement a pattern that does something like this: