Kotlin 中的 equal() 函数
我需要如果两个对象 equal()
需要 print("Equal")
如果对象不相等 -> “不等于”。我找不到这段代码的错误这是我在 IntelliJ IDEA 中的代码 附带说明一下,当我们重写 equals() 时,建议也重写 hashCode() 方法。如果我们不这样做,相同的对象可能会得到不同的哈希值;基于哈希的集合,包括 HashMap、HashSet 和 Hashtable 无法正常工作(有关更多详细信息,请参阅此处)。我们将在另一篇文章中介绍有关 hashCode() 的更多信息。 参考:
internal class Complex(private val re: Double, private val im: Double) {
// Overriding equals() to compare two Complex objects
fun equals(o: Object): Boolean {
// If the object is compared with itself then return true
if (o === this) {
return true
}
/* Check if o is an instance of Complex or not
"null instanceof [type]" also returns false */if (o !is Complex) {
return false
}
// typecast o to Complex so that we can compare data members
val c = o as Complex
// Compare the data members and return accordingly
return (java.lang.Double.compare(re, c.re) == 0
&& java.lang.Double.compare(im, c.im) == 0)
}
} // Driver class to test the Complex class
fun main(args: Array<String>) {
val c1 = Complex(12.0, 15.0)
val c2 = Complex(10.0, 15.0)
if (c1 == c2) {
println("Equal ")
} else {
println("Not Equal ")
}
}
I need if two objects are equal()
need to print("Equal")
if objects are not equal -> "Not equal".I can not find mistake of this codeThis is my code in IntelliJ IDEA
As a side note, when we override equals(), it is recommended to also override the hashCode() method. If we don’t do so, equal objects may get different hash-values; and hash based collections, including HashMap, HashSet, and Hashtable do not work properly (see this for more details). We will be covering more about hashCode() in a separate post.
References:
internal class Complex(private val re: Double, private val im: Double) {
// Overriding equals() to compare two Complex objects
fun equals(o: Object): Boolean {
// If the object is compared with itself then return true
if (o === this) {
return true
}
/* Check if o is an instance of Complex or not
"null instanceof [type]" also returns false */if (o !is Complex) {
return false
}
// typecast o to Complex so that we can compare data members
val c = o as Complex
// Compare the data members and return accordingly
return (java.lang.Double.compare(re, c.re) == 0
&& java.lang.Double.compare(im, c.im) == 0)
}
} // Driver class to test the Complex class
fun main(args: Array<String>) {
val c1 = Complex(12.0, 15.0)
val c2 = Complex(10.0, 15.0)
if (c1 == c2) {
println("Equal ")
} else {
println("Not Equal ")
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Kotlin 中,您可以使用 Any 而不是 Object。它不允许您测试您的类实例是否是对象,而只能测试任何。
此外,您无法覆盖
equals
,因为您没有使用override
关键字。参数必须是Any?
,而不是Object
。更改
为
另外,在这种情况下,您应该使用数据类,这样您就不必首先编写自己的
equals()
实现。将来,当您不使用数据类时,您可以使用 IDE 选项自动生成
equals
和hashcode
。In Kotlin, you use Any instead of Object. It will not allow you to test if your class instance is an Object, only Any.
Also, you are failing to override
equals
since you didn't use theoverride
keyword. The argument needs to beAny?
, notObject
.Change
to
Also, in this case, you should use a data class so you won't have to write your own
equals()
implementation in the first place.And in the future, when you aren't using a data class, you can use the IDE option to generate
equals
andhashcode
for you automatically.数据类会更有意义:
输出:不等于
A data class would make more sense:
Output: Not Equal