隐藏 SortedList 的 .Add 方法与使用带有 base.Add 的另一个方法名称
我有一个自定义类,它基本上是一个带有一些额外属性和方法的 SortedList。我想在添加新的键/值对时(即调用 .Add
方法时)进行一些额外的处理。我可以隐藏 .Add
方法或使用其他方法名称(例如:.AddPair
),然后在该方法中调用 base.Add
。首选方法?为什么?
隐藏.Add方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritence_Test
{
class Program
{
static void Main(string[] args)
{
DYseries d = new DYseries() { FieldName = "test" };
d.Add(new DateTime(2010, 12, 1), 2345);
d.Add(new DateTime(2010, 12, 5), 2340);
d.Add(new DateTime(2010, 12, 2), 2343);
Console.WriteLine("fieldName {0} \n count {1} \n max {2} \n min {3}", d.FieldName, d.Count(), d.Keys.Max(), d.Keys.Min());
}
}
class DYseries : SortedList<DateTime, double>
{
public string FieldName { get; set; }
new public void Add(DateTime date, double value)
{
base.Add(date,value);
// additional processing here
Console.WriteLine("Added date {0}.\n Max date: {1}",date, this.Keys.Max());
}
}
}
或
使用其他方法名称:
class Program
{
static void Main(string[] args)
{
DYseries d = new DYseries() { FieldName = "test" };
d.AddPair(new DateTime(2010, 12, 1), 2345);
d.AddPair(new DateTime(2010, 12, 5), 2340);
d.AddPair(new DateTime(2010, 12, 2), 2343);
d.AddPair(new DateTime(2010, 12, 9), 2348);
Console.WriteLine("fieldName {0} \n count {1} \n max {2} \n min {3}", d.FieldName, d.Count(), d.Keys.Max(), d.Keys.Min());
}
}
class DYseries : SortedList<DateTime, double>
{
public string FieldName { get; set; }
public void AddPair(DateTime date, double value)
{
base.Add(date,value);
// additional processing here
Console.WriteLine("Added date {0}.\n Max date: {1}",date, this.Keys.Max());
}
}
是否有首选方法?一种方法(隐藏?)可能会导致问题吗?
I have a custom class that is basically a SortedList with a few extra properties and methods. I would like to do some additional processing when a new key/value pair is added (i.e when the .Add
method is called). I can hide the .Add
method or use another method name (ex:.AddPair
) and then call base.Add
in that method. Preferred approach? Why?
Hide .Add method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritence_Test
{
class Program
{
static void Main(string[] args)
{
DYseries d = new DYseries() { FieldName = "test" };
d.Add(new DateTime(2010, 12, 1), 2345);
d.Add(new DateTime(2010, 12, 5), 2340);
d.Add(new DateTime(2010, 12, 2), 2343);
Console.WriteLine("fieldName {0} \n count {1} \n max {2} \n min {3}", d.FieldName, d.Count(), d.Keys.Max(), d.Keys.Min());
}
}
class DYseries : SortedList<DateTime, double>
{
public string FieldName { get; set; }
new public void Add(DateTime date, double value)
{
base.Add(date,value);
// additional processing here
Console.WriteLine("Added date {0}.\n Max date: {1}",date, this.Keys.Max());
}
}
}
or
using another method name:
class Program
{
static void Main(string[] args)
{
DYseries d = new DYseries() { FieldName = "test" };
d.AddPair(new DateTime(2010, 12, 1), 2345);
d.AddPair(new DateTime(2010, 12, 5), 2340);
d.AddPair(new DateTime(2010, 12, 2), 2343);
d.AddPair(new DateTime(2010, 12, 9), 2348);
Console.WriteLine("fieldName {0} \n count {1} \n max {2} \n min {3}", d.FieldName, d.Count(), d.Keys.Max(), d.Keys.Min());
}
}
class DYseries : SortedList<DateTime, double>
{
public string FieldName { get; set; }
public void AddPair(DateTime date, double value)
{
base.Add(date,value);
// additional processing here
Console.WriteLine("Added date {0}.\n Max date: {1}",date, this.Keys.Max());
}
}
Is there a preferred approach? Will one approach (hiding?) potentially cause problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用第二种方法。第一个破坏了良好的面向对象设计,并且您无法确定是否会调用您的方法或基类。考虑一下你的类的这种用法:
我从来没有遇到过使用 new 的有效理由;总有其他方法可以在不破坏良好的面向对象设计的情况下完成您想要的事情。
Use the second approach. The first breaks good OO design, and you're not going to be sure if your method will be called or the base class. Consider this use of your class:
I've never come across a valid reason to use
new
; there are always other ways to acomplish what you want without breaking good OO design.您是否考虑过在这里使用聚合而不是继承?
让您的 DYSeries 类实现 IDictionary、ICollection、IEnumerable、IDictionary、ICollection、IEnumerable,就像 SortedList 一样。然后拥有一个
SortedList
的私有成员实例,您可以将所有方法和属性实现委托给该实例,并添加额外的处理。这将确保不存在调用者无意中调用本机SortedList.Add
等的风险。您甚至可以更进一步,基于此方法创建一个通用基类,您可以从中派生未来的实现,该实现提供可以在核心方法之前和之后调用的虚拟函数。
Have you considered using aggregation rather than inheritance here?
Have your
DYSeries
class implementIDictionary<>, ICollection<>, IEnumerable<>, IDictionary, ICollection, IEnumerable
just as SortedList does. Then have a private member instance of aSortedList
to which you delegate all the method and property implementations, adding your additional processing. This will ensure that there is not a risk that the nativeSortedList.Add
etc. is invoked by the caller inadvertantly.You could even take this further and creaet a generic base class based on this approach, from which you can derive future implementation which provide virtual functions that can be called before and after the core methods.