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
匿名对象的方法范围-Kotlin_Kotlin - Fatal编程技术网

匿名对象的方法范围-Kotlin

匿名对象的方法范围-Kotlin,kotlin,Kotlin,在Kotlin中,如果我在匿名对象上定义了一个方法,有时我能够访问它,而有时我不能。这似乎与范围界定规则有关,但我不确定是什么 在下面的代码示例中,访问example3.field.method()将导致编译错误。有趣的是,example2.field.method() 以下行为的解释是什么 class Example3 { val field = object { fun method() {} } } fun showcase() { val exa

在Kotlin中,如果我在匿名对象上定义了一个方法,有时我能够访问它,而有时我不能。这似乎与范围界定规则有关,但我不确定是什么

在下面的代码示例中,访问
example3.field.method()
将导致编译错误。有趣的是,
example2.field.method()

以下行为的解释是什么

class Example3 {
    val field = object {
        fun method() {}
    }
}

fun showcase() {
    val example1 = object {
        fun method() {}
    }
    example1.method()
    println(example1::class.qualifiedName)

    class Example2 {
        val field = object {
            fun method() {}
        }
    }

    val example2 = Example2()
    example2.field.method()
    println(example2::class.qualifiedName)

    val example3 = Example3()
    // example3.field.method()  // won't compile
    println(example3::class.qualifiedName)
}
从文档:

请注意,匿名对象只能在本地和 私人声明。如果使用匿名对象作为返回类型 对于公共功能或公共属性的类型,实际类型 该函数或属性的 匿名对象,或
Any
,如果未声明任何超类型成员 添加到匿名对象中的内容将无法访问。

在下面的代码示例中演示:

class Example4{
    val publicObj = object{
        val x = 1
    }

    private val privateObj = object{
        val x = 2
    }

    fun showcase(){
        val scopedObj = object{
            val x = 3
        }
        println(publicObj.x)    // ERROR : unresolved reference: x
        println(privateObj.x)   // OK
        println(scopedObj.x)    // OK
    }
}
从文档:

请注意,匿名对象只能在本地和 私人声明。如果使用匿名对象作为返回类型 对于公共功能或公共属性的类型,实际类型 该函数或属性的 匿名对象,或
Any
,如果未声明任何超类型成员 添加到匿名对象中的内容将无法访问。

在下面的代码示例中演示:

class Example4{
    val publicObj = object{
        val x = 1
    }

    private val privateObj = object{
        val x = 2
    }

    fun showcase(){
        val scopedObj = object{
            val x = 3
        }
        println(publicObj.x)    // ERROR : unresolved reference: x
        println(privateObj.x)   // OK
        println(scopedObj.x)    // OK
    }
}

Pawel针对您的问题给出了正确的答案,并指出了文档:

该函数或属性的实际类型将是匿名对象的已声明超类型,或者如果没有声明任何超类型,则为任何类型

但是如果您真的需要访问
example3.field.method()
您可以在
example3
中声明一个超类型到
field

interface MyInterface {
    fun method()
}

class Example3 {
    val field = object: MyInterface { 
        override fun method() {} 
    }
}

fun main() {
    val example3 = Example3()
    example3.field.method()
}

Pawel针对您的问题给出了正确的答案,并指出了文档:

该函数或属性的实际类型将是匿名对象的已声明超类型,或者如果没有声明任何超类型,则为任何类型

但是如果您真的需要访问
example3.field.method()
您可以在
example3
中声明一个超类型到
field

interface MyInterface {
    fun method()
}

class Example3 {
    val field = object: MyInterface { 
        override fun method() {} 
    }
}

fun main() {
    val example3 = Example3()
    example3.field.method()
}