- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我定义了以下接口(interface):
public interface IHaveAProblem
{
string Issue { get; set; }
}
下面是IHaveAProblem
的实现:
public class SomeProblem : IHaveAProblem
{
public string Issue { get; set; }
public override bool Equals(object obj)
{
SomeProblem otherObj = obj as SomeProblem;
if (otherObj == null)
{
return false;
}
return this.Issue == otherObj.Issue;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public static bool operator ==(SomeProblem rhs, SomeProblem lhs)
{
// Null check
if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))
{
if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))
{
// Both are null. They do equal each other
return true;
}
// Only 1 is null the other is not so they do not equal
return false;
}
return rhs.Equals(lhs);
}
public static bool operator !=(SomeProblem rhs, SomeProblem lhs)
{
// Null check
if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))
{
if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))
{
// Both are null. They do equal each other
return false;
}
// Only 1 is null the other is not so they do not equal
return true;
}
return !rhs.Equals(lhs);
}
}
当我使用该对象时,我可以获得==
比较的正确结果:
SomeProblem firstTest = new SomeProblem()
{
Issue = "Hello World"
};
SomeProblem secondTest = new SomeProblem()
{
Issue = "Hello World"
};
// This is true
bool result = firstTest == secondTest;
但是,当我尝试比较接口(interface)时,它是在进行内存比较,而不是 SomeProblem
上的运算符 ==
:
IHaveAProblem firstProblem = new SomeProblem()
{
Issue = "Hello World"
};
IHaveAProblem secondProblem = new SomeProblem()
{
Issue = "Hello World"
};
是否可以让接口(interface)在 SomeProblem
上使用 ==
而不是内存比较?
我知道我可以执行 firstProblem.Equals(secondProblem)
并获得正确的结果。但是,我正在创建一个框架,我不会知道它最终是如何使用的。我认为 ==
会正常工作。
最佳答案
运算符==
是静态的。不能在 C# 中为接口(interface)定义静态方法。此外,对于所有运算符,至少有一个参数类型需要与定义它的类具有相同的类型,因此:接口(interface)没有运算符重载:(
您可以做的是改用抽象类 - 并在那里定义运算符。同样,运算符不能是虚拟的(因为静态方法不能是虚拟的...)
[已编辑,原因见评论。]
关于c# - 如何覆盖 C# 中接口(interface)的等号运算符 ==?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1881459/
我有一个代码: $("#button").click(function () { for (var i = 0; i < 4; i++) {
我一直在应该用 YAML 编写的 ansible 剧本中阅读 key1=value1 key2=value2 风格的字典。另一方面,我没有找到任何关于这种格式的文档,而且似乎在某些情况下它对我不起作用
我使用 $(this).attr("href"); 来选择被单击元素的 href 属性值,但我只需要选择最后一个 '= 之后的部分' 符号。 示例: href="index.php?con=123&i
我有一个查询需要 20 秒才能执行,请按照我的查询操作: SELECT MATLIGA.COD_MAT_FAMILIA FROM ORCAMENTOS.dbo.OR_1INSUMOS INSU
我使用 Visual studio 2012 和插件 Visual Assist X 版本。 1916.我有两个问题。 1) 当我写等号 (=) 时,我希望在符号前后自动插入空格(我必须始终在键盘上按
我有一个数据模板 xaml,目前看起来像这样用于报告屏幕
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: When to use the equals sign in a Scala method declaration?
嘿,我不确定为什么会这样。 GA 似乎随机地将等号附加到某些 URL。它们不会以这种方式出现在实时站点上。 我做了一个没有过滤器或其他配置的全新 View ,它仍然会发生。我以前从未见过这种情况,这很
是否可以在“=”表达式中转义 python f 字符串? (Python 3.8 中的新功能) 例如我有以下表达式: def double(x): return x * 2 some_int
是否可以在“=”表达式中转义 python f 字符串? (Python 3.8 中的新功能) 例如我有以下表达式: def double(x): return x * 2 some_int
使用 PHPMailer 5.2.14,电子邮件以文本/html 格式发送。传出的文本每隔 75 个字符就会散布一些等号。 我尝试使用 EOL workaround , 但它没有删除多余的等号: $e
如果 C++ 中包含等号,我如何解析命令行选项?一个例子是: ./testApp --parameter1=value1 我使用以下行来检查参数是否存在: bool cmdOptionExists(c
我在创建带有索引的迭代器时碰巧犯了一个复制粘贴错误,如下所示。 ... 正确的版本应该在索引语句中包含等号而不是 of。 问题不在于如何获取索引。 我感兴趣的是我得到了[Object object],
我正在自动执行 RSA 软 token 转换并将转换字符串传递给 Outlook。我意识到我无法使用批处理将字符串变成 URL 到 outlook 命令行,但这不是我的问题。我的问题是将讨厌的等号转换
我正在关注 Baking Pi剑桥大学的类(class),其中基于 ARMv6 指令集构建了一个简单的操作系统,针对 Raspberry Pi。 到目前为止,我们一直在使用两种通过 ldr 指令将数据
我是一名优秀的程序员,十分优秀!