隐藏 SortedList 的 .Add 方法与使用带有 base.Add 的另一个方法名称

发布于 2024-10-19 20:31:10 字数 2124 浏览 0 评论 0原文

我有一个自定义类,它基本上是一个带有一些额外属性和方法的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有心的人 2024-10-26 20:31:10

使用第二种方法。第一个破坏了良好的面向对象设计,并且您无法确定是否会调用您的方法或基类。考虑一下你的类的这种用法:

SortedList<DateTime, double> myList = new DYseries();
myList.Add(date, value);  // This will call the base, not your implementation!

我从来没有遇到过使用 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:

SortedList<DateTime, double> myList = new DYseries();
myList.Add(date, value);  // This will call the base, not your implementation!

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.

感情旳空白 2024-10-26 20:31:10

您是否考虑过在这里使用聚合而不是继承?

让您的 DYSeries 类实现 IDictionary、ICollection、IEnumerable、IDictionary、ICollection、IEnumerable,就像 SortedList 一样。然后拥有一个 SortedList 的私有成员实例,您可以将所有方法和属性实现委托给该实例,并添加额外的处理。这将确保不存在调用者无意中调用本机 SortedList.Add 等的风险。

您甚至可以更进一步,基于此方法创建一个通用基类,您可以从中派生未来的实现,该实现提供可以在核心方法之前和之后调用的虚拟函数。

Have you considered using aggregation rather than inheritance here?

Have your DYSeries class implement IDictionary<>, ICollection<>, IEnumerable<>, IDictionary, ICollection, IEnumerable just as SortedList does. Then have a private member instance of a SortedList 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 native SortedList.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文