Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 可变表集don';使用密封类时不能防止重复内容_Kotlin - Fatal编程技术网

Kotlin 可变表集don';使用密封类时不能防止重复内容

Kotlin 可变表集don';使用密封类时不能防止重复内容,kotlin,Kotlin,如果我将MutableSet与sealed class一起使用,MutableSet接受所有重复的内容 样本: sealed class LoginSavedCommand { class Login(val email: String, val password: String) : LoginSavedCommand() class SaveData(val email: String, val password: String) : LoginSavedCommand()

如果我将
MutableSet
sealed class
一起使用,
MutableSet
接受所有重复的内容

样本:

sealed class LoginSavedCommand {
    class Login(val email: String, val password: String) : LoginSavedCommand()
    class SaveData(val email: String, val password: String) : LoginSavedCommand()
}

fun main(args: Array<String>) {

    val mSet: MutableSet<LoginSavedCommand> = hashSetOf()

    mSet.add(LoginSavedCommand.Login("oba", "pass"))
    mSet.add(LoginSavedCommand.Login("faiii", "blabla"))

    if (mSet.add(LoginSavedCommand.Login("oba", "pass"))) {
        println("don't")
    } else {
        println("do")
    }
}
密封类LoginSavedCommand{
类登录(val电子邮件:String,val密码:String):LoginSavedCommand()
类SaveData(val电子邮件:String,val密码:String):LoginSavedCommand()
}
趣味主线(args:Array){
val mSet:MutableSet=hashSetOf()
mSet.add(LoginSavedCommand.Login(“oba”、“pass”))
mSet.add(LoginSavedCommand.Login(“faiii”、“blabla”))
if(mSet.add(LoginSavedCommand.Login(“oba”、“pass”)){
println(“不要”)
}否则{
println(“do”)
}
}

我将相同的值传递给了
LoginSavedCommand.Login
,但是
MutableSet
继续接受添加相同的值(在示例
println
println上,我需要打印“do”,因为我需要使用该
selaed类防止重复内容)

A
MutableSet
使用元素的和检查它是否包含元素,具体取决于实现。例如,
HashSet
使用
hashCode
存储并快速查找哈希表中的元素

在您的示例中,
密封类
的两个子类不重写
equals
函数,因此提供默认的相等检查实现,即标识相等(即,一个对象仅与自身相等,而不同的对象即使其属性相等也永远不相等)

要实现
LoginSavedCommand
项目在
MutableSet
中的唯一性,需要确保子类提供适当的相等性检查实现


一种简单的方法是,编译器根据以下属性生成
equals
hashCode
实现:

sealed class LoginSavedCommand {
    data class Login(val email: String, val password: String) : LoginSavedCommand()
    data class SaveData(val email: String, val password: String) : LoginSavedCommand()
}


或者,在子类中手动重写
equals
hashCode
函数

重要提示:重写这些函数时,请确保实现遵循API参考和中描述的函数契约

例如:

sealed class LoginSavedCommand {
    class Login(val email: String, val password: String) : LoginSavedCommand() {
        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            if (javaClass != other?.javaClass) return false

            other as Login

            if (email != other.email) return false
            if (password != other.password) return false

            return true
        }

        override fun hashCode(): Int {
            var result = email.hashCode()
            result = 31 * result + password.hashCode()
            return result
        }
    }


    class SaveData(val email: String, val password: String) : LoginSavedCommand() {
        /* ... */
    }
}

这些实现由IntelliJ IDEA生成,使用
Generate…
equals()和hashCode()
类主体内的操作。

我不知道这个密封类在该上下文中的作用。但是使用数据类(自动创建hashCode和equals方法)怎么样呢。。我猜在你的案子里没有;-)@IEE1394我需要这个密封的类,因为我将在协同程序上使用它伟大且组织良好的答案!!!我测试了一下,它成功了。我认为只将数据类保存在密封类中是足够的,我只需要这个类类型将值传递给协程参与者。