【发布时间】:2016-03-26 12:26:37
【问题描述】:
我有一个项目,其中有使用模板的 State 类。 我有一个类 Cell,我将它用作 State,因此 State 将 Cell 作为 genericState。 现在我有一个通用函数来检查两个实例是否相等。 问题是,它永远不会将 State Equals 方法留给 Cell Equals 方法。
public class State<T>
{
public T genericState; //in my case T is a cell
public State(T cellState) // CTOR
{
this.genericState = cellState;
}
public override bool Equals(object obj)
{
return genericState.Equals((obj as State<T>).genericState);
} //never leaves
}
和单元格的代码,它永远不会得到:
public class Cell
{
public int row, col;
public bool visited;
public char value;
public bool Equals(Cell other) //never gets here
{
return other != null && other.row == row && other.col == col;
}
}
我不明白为什么它永远不会到达 Cell 的 Equal 方法。代码可能有什么问题?
【问题讨论】:
-
我想this 可能会对你有所帮助
标签: c# templates generics equals