赞
踩
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/116668666
本文出自【赵彦军的博客】
Kotlin实战指南十六:Synchronized、Volatile
Kotlin实战指南十五:协程泄漏
Kotlin实战指南十四:协程启动模式
JvmField
是针对kotlin
字段来说的,有两重意思:
class Util {
val age = 1
}
转换成 java 类
public final class Util {
private final int age = 1;
public final int getAge() {
return this.age;
}
}
可以看到,a1
是私有的,并且对外暴露了 getAge
方法,如果 age
是 var
类型,还会暴露 setAge
方法。
下面我们加上 JvmField 注解后,再看看
class Util {
@JvmField
var age = 1
}
转成 java 文件
public final class Util {
@JvmField
public int age = 1;
}
通过前后对比,我们可以得出结论
只能在object类
或者伴生对象companion object
中使用,而@JvmField
没有这些限制
JvmStatic 作用:
object类
或者伴生对象companion object
中使用get / set
方法变成静态的object Util {
var age = 1
fun run() {
}
}
转成 java
public final class Util { private static int age; @NotNull public static final Util INSTANCE; public final int getAge() { return age; } public final void setAge(int var1) { age = var1; } public final void run() { } private Util() { } static { Util var0 = new Util(); INSTANCE = var0; age = 1; } }
加上 JvmStatic 后再观察
object Util {
@JvmStatic
var age = 1
@JvmStatic
fun run() {
}
}
转换成 java 后
public final class Util { private static int age; @NotNull public static final Util INSTANCE; /** @deprecated */ // $FF: synthetic method @JvmStatic public static void age$annotations() { } public static final int getAge() { return age; } public static final void setAge(int var0) { age = var0; } @JvmStatic public static final void run() { } private Util() { } static { Util var0 = new Util(); INSTANCE = var0; age = 1; } }
class Util {
companion object {
var age = 1
fun run() {
}
}
}
转成 java 后
public final class Util { private static int age = 1; @NotNull public static final Util.Companion Companion = new Util.Companion((DefaultConstructorMarker)null); public static final class Companion { public final int getAge() { return Util.age; } public final void setAge(int var1) { Util.age = var1; } public final void run() { } private Companion() { } // $FF: synthetic method public Companion(DefaultConstructorMarker $constructor_marker) { this(); } } }
加上 JvmStatic 后,我们再观察
class Util {
companion object {
@JvmStatic
var age = 1
@JvmStatic
fun run() {
}
}
}
转成 java 后
public final class Util { private static int age = 1; @NotNull public static final Util.Companion Companion = new Util.Companion((DefaultConstructorMarker)null); public static final int getAge() { Util.Companion var10000 = Companion; return age; } public static final void setAge(int var0) { Util.Companion var10000 = Companion; age = var0; } @JvmStatic public static final void run() { Companion.run(); } public static final class Companion { /** @deprecated */ // $FF: synthetic method @JvmStatic public static void age$annotations() { } public final int getAge() { return Util.age; } public final void setAge(int var1) { Util.age = var1; } @JvmStatic public final void run() { } private Companion() { } // $FF: synthetic method public Companion(DefaultConstructorMarker $constructor_marker) { this(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。