如何访问Koltin中的静态伴侣对象的实例variables
我正在尝试在kotlin
执行网络操作的kotlin
。 我有下面的代码主要构造函数正在采取Command
和Context
。
我无法访问command.execute(JSONObject(jsonObj))
命令variables,得到以下错误。 我不确定是什么原因造成的问题?
未解决的参考:命令
class AsyncService(val command: Command, val context: Context) { companion object { fun doGet(request: String) { doAsync { val jsonObj = java.net.URL(request).readText() command.execute(JSONObject(jsonObj)) } } } }
伴随对象不是类的实例的一部分。 您不能从协同对象访问成员,就像在Java中一样,您无法从静态方法访问成员。
相反,不要使用随播广告对象:
class AsyncService(val command: Command, val context: Context) { fun doGet(request: String) { doAsync { val jsonObj = java.net.URL(request).readText() command.execute(JSONObject(jsonObj)) } } }
您应该直接将parameter passing给您的伴侣对象函数:
class AsyncService { companion object { fun doGet(command: Command, context: Context, request: String) { doAsync { val jsonObj = java.net.URL(request).readText() command.execute(JSONObject(jsonObj)) } } } }