比较两个自定义 类
Comparing two custom classes
我正在尝试比较我创建的两个对象。
在其他一些帖子中, class 需要继承 IEquatable<T>
接口,然后覆盖 Equals
功能。当我比较 class 和 cellStyle1.Equals(cellStyle2)
的实例时,它总是 returns false
...
我试过了,好像不行。这是我尝试过的:
class Cell_Style: IEquatable<Cell_Style>
{
private Color cellColor;
private Color fontColor;
private int fontSize;
private bool underline;
private bool bold;
private bool italic;
public int FontSize { get => fontSize; set => fontSize = value; }
public Color FontColor { get => fontColor; set => fontColor = value; }
public Color CellColor { get => cellColor; set => cellColor = value; }
public bool Underline { get => underline; set => underline = value; }
public bool Bold { get => bold; set => bold = value; }
public bool Italic { get => italic; set => italic = value; }
public Cell_Style(Excel.Range cell)
{
cellColor = ColorTranslator.FromOle(Convert.ToInt32(cell.Interior.Color));
underline = (cell.Font.Underline == (int)Excel.XlUnderlineStyle.xlUnderlineStyleSingle) ? true : false;
bold = cell.Font.Bold;
fontColor = ColorTranslator.FromOle(Convert.ToInt32(cell.Font.Color));
fontSize = Convert.ToInt32(cell.Font.Size);
italic = cell.Font.Italic;
}
//1st attempt
bool IEquatable<Cell_Style>.Equals(Cell_Style other)
{
return Equals(other);
}
//2nd attempt
bool IEquatable<Cell_Style>.Equals(Cell_Style other)
{
if (other.Bold == bold &&
other.CellColor == cellColor &&
other.FontColor == fontColor &&
other.FontSize == fontSize &&
other.Underline == underline &&
other.Italic == italic)
{
return true;
}
else
{
return false;
}
}
}
我做错了什么?
您调用了从 System.Object
继承的 Equals
方法,而不是您的实现。您应该重写基础 Equals
方法,最好使用隐式接口实现(而不是显式),这样如果编译器可以解析此调用(cellStyle1
和 cellStyle2
都是编译时 Cell_Style
类型)。
您正在显式实现 IEquatable<Cell_Style>
,因此您需要在调用之前强制转换为该接口:
((IEquatable<Cell_Style>)cellStyle1).Equals(cellStyle2)
一个更简单的方法是使实现 public:
public bool Equals(Cell_Style other) { ... }
如果 cellStyle2
的类型为 Cell_Style
,那么将选择 Equals
的正确重载。您还应该覆盖 object.Equals
以调用 Equals(Cell_Style)
并实施 GetHashCode
.
我正在尝试比较我创建的两个对象。
在其他一些帖子中, class 需要继承 IEquatable<T>
接口,然后覆盖 Equals
功能。当我比较 class 和 cellStyle1.Equals(cellStyle2)
的实例时,它总是 returns false
...
我试过了,好像不行。这是我尝试过的:
class Cell_Style: IEquatable<Cell_Style>
{
private Color cellColor;
private Color fontColor;
private int fontSize;
private bool underline;
private bool bold;
private bool italic;
public int FontSize { get => fontSize; set => fontSize = value; }
public Color FontColor { get => fontColor; set => fontColor = value; }
public Color CellColor { get => cellColor; set => cellColor = value; }
public bool Underline { get => underline; set => underline = value; }
public bool Bold { get => bold; set => bold = value; }
public bool Italic { get => italic; set => italic = value; }
public Cell_Style(Excel.Range cell)
{
cellColor = ColorTranslator.FromOle(Convert.ToInt32(cell.Interior.Color));
underline = (cell.Font.Underline == (int)Excel.XlUnderlineStyle.xlUnderlineStyleSingle) ? true : false;
bold = cell.Font.Bold;
fontColor = ColorTranslator.FromOle(Convert.ToInt32(cell.Font.Color));
fontSize = Convert.ToInt32(cell.Font.Size);
italic = cell.Font.Italic;
}
//1st attempt
bool IEquatable<Cell_Style>.Equals(Cell_Style other)
{
return Equals(other);
}
//2nd attempt
bool IEquatable<Cell_Style>.Equals(Cell_Style other)
{
if (other.Bold == bold &&
other.CellColor == cellColor &&
other.FontColor == fontColor &&
other.FontSize == fontSize &&
other.Underline == underline &&
other.Italic == italic)
{
return true;
}
else
{
return false;
}
}
}
我做错了什么?
您调用了从 System.Object
继承的 Equals
方法,而不是您的实现。您应该重写基础 Equals
方法,最好使用隐式接口实现(而不是显式),这样如果编译器可以解析此调用(cellStyle1
和 cellStyle2
都是编译时 Cell_Style
类型)。
您正在显式实现 IEquatable<Cell_Style>
,因此您需要在调用之前强制转换为该接口:
((IEquatable<Cell_Style>)cellStyle1).Equals(cellStyle2)
一个更简单的方法是使实现 public:
public bool Equals(Cell_Style other) { ... }
如果 cellStyle2
的类型为 Cell_Style
,那么将选择 Equals
的正确重载。您还应该覆盖 object.Equals
以调用 Equals(Cell_Style)
并实施 GetHashCode
.