Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/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
Generics 有没有与C#'等价的东西;s";“违约”;Kotlin的关键字?_Generics_Kotlin - Fatal编程技术网

Generics 有没有与C#'等价的东西;s";“违约”;Kotlin的关键字?

Generics 有没有与C#'等价的东西;s";“违约”;Kotlin的关键字?,generics,kotlin,Generics,Kotlin,代码示例: import java.util.UUID interface InterfaceOne<AType> { var abcOne:AType } interface InterfaceTwo<AType> { var abcTwo:AType } class Example<AType>: InterfaceOne<AType>, InterfaceTwo<AType> { override

代码示例:

import java.util.UUID

interface InterfaceOne<AType> {
    var abcOne:AType
}

interface InterfaceTwo<AType> {
    var abcTwo:AType
}

class Example<AType>: InterfaceOne<AType>, InterfaceTwo<AType> {

    override var abcOne: AType // Looking for default value to not from constructor 
    set(value) {
        field = value
        //...
    }

    override var abcTwo: AType // Looking for default value to not from constructor 
    set(value) {
        field = value
            //...
    }

   fun test(uuid: AType) {
       abcTwo = uuid
       abcOne = default // I'm looking for C#'s default keyword equivalent in here
   }
}

fun main() {

    val uuid = UUID.randomUUID()
    val uuid2 = UUID.randomUUID()

    val interfaceOne = Example<UUID>()

    interfaceOne.test(uuid)
}
import java.util.UUID

interface InterfaceOne.

我认为Kotlin没有同等的功能。在科特林没有什么可抱怨的。Kotlin不会将类字段初始化为任何默认值,甚至不会初始化原语(与Java不同,例如,
int
被初始化为0)

您必须在主构造函数中或在声明时初始化字段,或者使其可为null并保持未初始化状态(null)

Kotlin允许您为构造函数中的参数值指定默认值,但这需要是一个具体类的实例,因此不能考虑泛型

您可以使用factory方法生成默认值:

class Example<AType>(val factory: () -> AType): InterfaceOne<AType>, InterfaceTwo<AType> {

    override var abcOne: AType = factory()

    override var abcTwo: AType = factory()

    fun test(uuid: AType) {
        abcTwo = uuid
        abcOne = factory()
        println("abcOne=$abcOne")
        println("abcTwo=$abcTwo")
    }
}

fun main() {

    val uuid = UUID.randomUUID()
    val uuidExample = Example<UUID>({UUID.randomUUID()})
    uuidExample.test(uuid)

    val stringExample = Example<String>({"default"})
    stringExample.test("two")
}
类示例(val工厂:()->AType):InterfaceOne,InterfaceTwo{
重写变量abcOne:AType=factory()
重写变量abcTwo:AType=factory()
趣味测试(uuid:AType){
abcTwo=uuid
abcOne=工厂()
println(“abcOne=$abcOne”)
println(“abcTwo=$abcTwo”)
}
}
主要内容(){
val uuid=uuid.randomUUID()
val uuidExample=示例({UUID.randomuid()})
uuid示例测试(uuid)
val stringExample=Example({“default”})
stringExample.test(“两个”)
}

你好,达里奥!谢谢你回答我的问题!我已经想到了主构造函数,但是如果AType考虑“类类型”或“对象”或“原语类型”,我应该如何使这些对象作为缺省初始化?你有什么建议?如果你有时间,你想在代码中给我看吗?如果你那样做,我会很高兴见到你的。谢谢可以做的一件事是向类传递一个返回“detault”值的函数。看看这个。我还将用一些代码编辑我的答案。谢谢你的代码!我还有一个问题。此实现可以作为“注释”实现吗?例如,有一个名为“DefaultInitializer”的注释,它的目标是“AnnotationTarget.CLASS”类型、“AnnotationTarget.PROPERTY”、“AnnotationTarget.FIELD”和“AnnotationTarget.TYPEALIAS”等。它有意义吗?你怎么看?不客气。我认为你不能把工厂放在注释中。您可以将它放在另一个接口中:
Interface DefaultValueFactory{val factory:()->AType}