如何引用外部对象的属性?

问题描述 投票:0回答:1

考虑以下代码:

class SomeClass {

    companion object {
        val str = "My String"
        object AnotherObject {
             fun foo(str: String) {
                  return str + //companion object's str
             }
        }
    }
}

如何从

SomeClass::companion object::str
引用
AnotherObject::foo

kotlin object scope companion-object
1个回答
0
投票

请尝试如下:

class SomeClass {
    companion object {
        val str = "My String"
        object AnotherObject {
            fun foo(str: String): String {
                return str + SomeClass.str
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.