从另一个类调用比较方法
本文关键字:比较 方法 调用 另一个 | 更新日期: 2023-09-27 18:18:48
请帮我解决以下问题:首先,我有一个类
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比较方法。
A objA = new A();
BinarySearchTree<A> bst = new BinarySearchTree<A>();
Bst.Add(objA);
问题是这两个类在不同的库中。BinarySearchTree是一个泛型类。那么我如何比较一个类型的对象在添加函数的BinarySearchTree类。
我更新了这篇文章以得到更多的澄清。让我更清楚地描述我的问题。我有一些商业规则。并且根据不同的业务规则,我需要构造不同类型的BST。当然,BST在相同的逻辑上生成,但在不同的比较逻辑上生成。
假设,我有BusineesRules程序集。它包含四种规则,如Business1, Business2, Business3。Business4。我有一个程序集来生成BST和其他遍历方法。这个BST是完全通用的和组装的BST现在我需要在每个类中实现比较方法。, .
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
**添加泛型比较器方法。下面的类被添加到业务规则类集中。
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 = compare . compare(当前). "值,数据);"但它是二进制搜索类的比较。我可以调用比较方法business1或business2。这个比较器实现对比较BinarySearchTree.Add()方法中的对象有用吗?更重要的是,BST和其他业务规则类是不同的程序集。
如果你想调用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)
}