Kotlin 中的 equal() 函数

发布于 2025-01-12 16:10:17 字数 1423 浏览 4 评论 0原文

我需要如果两个对象 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟凡古楼 2025-01-19 16:10:17

在 Kotlin 中,您可以使用 Any 而不是 Object。它不允许您测试您的类实例是否是对象,而只能测试任何。

此外,您无法覆盖 equals,因为您没有使用 override 关键字。参数必须是 Any?,而不是 Object

更改

fun equals(o: Object): Boolean {

override fun equals(o: Any?): Boolean {

另外,在这种情况下,您应该使用数据类,这样您就不必首先编写自己的 equals() 实现。

将来,当您不使用数据类时,您可以使用 IDE 选项自动生成 equalshashcode

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 the override keyword. The argument needs to be Any?, not Object.

Change

fun equals(o: Object): Boolean {

to

override fun equals(o: Any?): Boolean {

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 and hashcode for you automatically.

清音悠歌 2025-01-19 16:10:17

数据类会更有意义:

data class Complex(
  private val re: Double,
  private val im: Double
)

val c1 = Complex(12.0, 15.0)
val c2 = Complex(10.0, 15.0)

if (c1 == c2) {
  println("Equal")
} else {
  println("Not Equal")
}

输出:不等于

A data class would make more sense:

data class Complex(
  private val re: Double,
  private val im: Double
)

val c1 = Complex(12.0, 15.0)
val c2 = Complex(10.0, 15.0)

if (c1 == c2) {
  println("Equal")
} else {
  println("Not Equal")
}

Output: Not Equal

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文