我正在遵循MSDN 的价值平等指南,我发现了一个文档没有涵盖的案例,即基类的平等。
一点背景:
我正在开发麻将游戏(4 人,不匹配),并且正在定义牌。牌可以分为两组:套装,有一个与之相关的数字(并且可以按顺序放在一起,如 2-3-4)和荣誉牌,没有编号。
这是我到目前为止所拥有的:
public enum MahjongSuitType
{
Bamboo = 1,
Character,
Dot
}
public enum MahjongSuitNumber
{
One = 1,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine
}
public enum MahjongHonorType
{
GreenDragon = 1,
RedDragon,
WhiteDragon,
EastWind,
SouthWind,
WestWind,
NorthWind
}
public abstract class MahjongTile
{
}
public class MahjongSuitTile : MahjongTile, IEquatable<MahjongTile>
{
public MahjongSuitType SuitType { get; private set; }
public MahjongSuitNumber SuitNumber { get; private set; }
public bool IsRedBonus { get; private set; } //this has no bearing on equality
public MahjongSuitTile(MahjongSuitType suitType,
MahjongSuitNumber suitNumber,
bool isRedBonus = false)
{
this.SuitType = suitType;
this.SuitNumber = suitNumber;
this.IsRedBonus = isRedBonus;
}
public override bool Equals(object obj)
{
return this.Equals(obj as MahjongTile);
}
public bool Equals(MahjongTile other)
{
if (Object.ReferenceEquals(other, null))
return false;
if (Object.ReferenceEquals(other, this))
return true;
MahjongSuitTile otherSuitTile = other as MahjongSuitTile;
if (Object.ReferenceEquals(otherSuitTile, null))
return false;
return (this.SuitType == otherSuitTile.SuitType) &&
(this.SuitNumber == otherSuitTile.SuitNumber);
}
public override int GetHashCode()
{
return this.SuitType.GetHashCode() ^ this.SuitNumber.GetHashCode();
}
}
public class MahjongHonorTile : MahjongTile, IEquatable<MahjongTile>
{
public MahjongHonorType HonorType { get; private set; }
public MahjongHonorTile(MahjongHonorType honorType)
{
this.HonorType = HonorType;
}
public override bool Equals(object obj)
{
return this.Equals(obj as MahjongTile);
}
public bool Equals(MahjongTile other)
{
if (Object.ReferenceEquals(other, null))
return false;
if (Object.ReferenceEquals(other, this))
return true;
MahjongHonorTile otherHonorTile = other as MahjongHonorTile;
if (Object.ReferenceEquals(otherHonorTile, null))
return false;
return this.HonorType == otherHonorTile.HonorType;
}
public override int GetHashCode()
{
return this.HonorType.GetHashCode();
}
}
对于大部分代码,我想通过基类引用磁贴,例如:
List<MahjongTile> hand = new List<MahjongTile>() { ... };
HashSet<MahjongTile> dragonTiles = new HashSet()
{
new MahjongHonorTile(MahjongHonorType.GreenDragon),
new MahjongHonorTile(MahjongHonorType.RedDragon),
new MahjongHonorTile(MahjongHonorType.WhiteDragon)
}
IEnumerable<MahjongTile> dragonTilesInHand = hand.Where(t => dragonTiles.Contains(t));
我的问题:我应该如何在MahjongTile
基类中定义平等?