我在文件myClass.kt中定义了一个var b,它不包含任何类,应用程序可以正常运行。
似乎var b就像Java中的static var一样,对吧?
主要
package com.example.dagger.kotlin.ui
class HomeActivity : DemoActivity() {
@Inject
lateinit var locationManager: LocationManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
(application as DemoApplication).component.inject(this)
// TODO Do something with the injected dependencies here
locationInfo.text = "Injected LocationManager:\n$locationManager"
Toast.makeText(this, b, Toast.LENGTH_LONG).show();
}
}
myClass.kt
package com.example.dagger.kotlin.ui
var b="New Girl"
答案 0 :(得分:2)
如果您反编译生成的.class
文件,您将获得:
public final class myClassKt {
@NotNull
public static final String getB() { return b; }
public static final void setB(@NotNull String b) { ... }
@NotNull
private static String b = "New Girl";
}
答案是,你得到一个private static
字段,其static
访问方法(获取和设置)