我在科特林上课:
class AClass {
companion object {
const val CONST_VAL = "THIS IS A CONST VAL STRING"
val JUST_VAL = "THIS IS A NON-CONST VAL STRING"
fun aFunction() {}
}
}
和Java中访问同伴成员的Main类:
public class Main {
public static void main(String[] args) {
// aFunction can only be accessed by using Companion
AClass.Companion.aFunction();
// CONST_VAL can only be accessed from the parent class
String constValString = AClass.CONST_VAL;
// JUST_VAL can only be accessed with Companion
String valString = AClass.Companion.getJUST_VAL();
}
}
在Java中,#aFunction()
和JUST_VAL
只能通过Companion
访问,而CONST_VAL
只能直接通过父类访问吗?也不应该仅通过CONST_VAL
访问Companion
吗?