从另一个类调用Compare Method

时间:2011-12-24 08:11:14

标签: c# .net oop

请帮我解决以下问题: 首先,我有一个班级

namespace ProbA
{

    public class A : IComparer
    {
        Private int a;
        public int IComparer.Compare(object CurrentNode, object DataNode)
        {
            WBPMember Current = (WBPMember)CurrentNode;
            WBPMember Data = (WBPMember)DataNode;
            return Current.a- Data.a;
        }
    }
}



 namespace BST
{

public class BinarySearchTree<T> : ICollection<T>, IEnumerable<T>
    {
public virtual void Add(T data)
        {
            // create a new Node instance
            BinaryTreeNode<T> n = new BinaryTreeNode<T>(data);
            int result;

            BinaryTreeNode<T> current = root, parent = null;
            while (current != null)
            {
      result = /**** I need to call the Class A compare method.     
     }
}
    }
}

我想在ProbA程序集中跟随语句执行时执行ClassA Compare方法。

A objA = new A();
BinarySearchTree<A> bst = new BinarySearchTree<A>();
Bst.Add(objA);

问题是两个类都在不同的库中。而BinarySearchTree是通用类。那么如何比较BinarySearchTree类的Add函数中的一个类型对象。


我更新此帖子以获得更多说明。 让我更清楚地描述我的问题。 我有一些类型的商业规则。根据不同的业务规则,我需要构建不同类型的BST。 Offcouse,BST以相同的逻辑产生,但是在不同的比较逻辑上。

让我们说,我有BusineesRules汇编。它包含4种规则,如Business1,Business2,Business3。 Business4。 我有一个用于生成BST和其他遍历方法的程序集。该BST完全通用且在装配BST中 现在我需要在每个类中实现compare方法。 像。

Public Class Business1: IComparer
{
    // Implemetation of Icompare.
}

所有其他课程相同。

当我尝试在BST中添加Business1的实例时,根据自己的实现进行比较,对其他类进行比较。

BST代码如下:

public class BinarySearchTree<T> : ICollection<T>, IEnumerable<T>
    {
        #region "Private Member Variables"
        private BinaryTreeNode<T> root = null;
        private int count = 0;
        private IComparer<T> comparer = Comparer<T>.Default;    // used to compare node values when percolating down the tree
        #endregion

        #region Constructors
        public BinarySearchTree() { }
        public BinarySearchTree(IComparer<T> comparer)
        {
            this.comparer = comparer;
        }
        #endregion

public virtual void Add(T data)
        {
            // create a new Node instance
            BinaryTreeNode<T> n = new BinaryTreeNode<T>(data);
            int result;

            // now, insert n into the tree
            // trace down the tree until we hit a NULL
            BinaryTreeNode<T> current = root, parent = null;
            while (current != null)
            {
                result = comparer.Compare(current.Value, data);                
                if (result == 0)
                    // they are equal - attempting to enter a duplicate - do nothing
                    return;
                else if (result > 0)
                {
                    // current.Value > data, must add n to current's left subtree
                    parent = current;
                    current = current.Left;
                }
                else if (result < 0)
                {
                    // current.Value < data, must add n to current's right subtree
                    parent = current;
                    current = current.Right;
                }
            }

            // We're ready to add the node!
            count++;
            if (parent == null)
                // the tree was empty, make n the root
                root = n;
            else
            {
                result = comparer.Compare(parent.Value, data);
                if (result > 0)
                    // parent.Value > data, therefore n must be added to the left subtree
                    parent.Left = n;
                else
                    // parent.Value < data, therefore n must be added to the right subtree
                    parent.Right = n;
            }
        }
        #endregion

* * 添加通用比较器方法。 以下类将添加到businessrule类程序集中。

class BusinessRules: IComparer
    {

        public int Compare(object CurrentHNode, object DataNode)
        {
            TreeMember Current;
            TreeMember Data;

            if (CurrentHNode is Current)
            {
                Current = (TreeMember)CurrentHNode;
            }
            else
                throw new ArgumentException("Object is not type of WBPMember");

            if (DataNode is Data)
            {
                Data = (TreeMember)DataNode;
            }
            else
                throw new ArgumentException("Object is not type of WBPMember");

            return Current.TreeIndex - Data.TreeIndex;
        }
    }

您可以在代码中看到以下行。 “result = comparer.Compare(current.Value,data);” 但它是二元搜索类的比较。所以我可以调用比较方法business1或business2。 这个比较器实现对比较BinarySearchTree.Add()方法中的对象有用吗? 更重要的是BST和其他businessrule类是不同的程序集。

1 个答案:

答案 0 :(得分:2)

如果你想调用Compare,你的BinarySearchTree类将需要引用比较器的实例。假设您希望使二进制搜索树保持一致,对于作用于搜索树的单个实例的所有操作使用相同的比较器似乎是合乎逻辑的。我建议:

  • 您更改了A课程以实施IComparer<WBPMember>而非IComparer
  • 您在BinarySearchTree中创建一个构造函数,该构造函数接受IComparer<T>并稍后记住它。
  • 您可以选择使用BinarySearchTree
  • 的默认比较器在T中创建无参数构造函数

这样的事情:

public class BinarySearchTree<T> : ICollection<T>, IEnumerable<T>
{
    private readonly IComparer<T> comparer;

    public BinarySearchTree(IComparer<T> comparer)
    {
        // TODO: Work out how to handle comparer == null (could throw an
        // exception, could use the default comparer).
        this.comparer = comparer;
    }

    public BinarySearchTree() : this(Comparer<T>.Default)
    {
    }

    // Now within Add, you can call comparer.Compare(current.Value, data)
}