- JvmField (用來(lái)修飾屬性)
使用jvmField注解,使得Kotlin編譯器不再對(duì)屬性字段生成getter/setter
class JvmFieldBean{
var name:String?=null
@JvmField var age:Int? =null
}
class Test{
public static void main(String[] args){
JvmFieldBean bean = new JvmFieldBean();
bean.setName("aaaa");//訪問(wèn)屬性只能通過(guò)setter和getter方法
bean.age = 20;//age可見(jiàn)性為public同時(shí)沒(méi)有生成setter和getter方法
}
}
故對(duì)應(yīng)的java代碼為:
class JvmFieldBean{
private String name;
public int age;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
- JvmName
使用JvmName來(lái)告訴編譯器生成的java類或者方法的名稱
@file:JvmName("JvmNameTest")
package com.today07
var name: String? = null
@JvmName("setUserName")
set(value) {
field = value
}
@JvmName("getInfos")
fun getInfo() {
}
class Test{
public static void main(String[] args){
JvmNameTest.setUserName("aaaa");//設(shè)置name屬性值只能通過(guò)setUserName方法
JvmNameTest.getName();//獲得該屬性的值
JvmNameTest.getInfos();//調(diào)用發(fā)現(xiàn)沒(méi)有g(shù)etInfo的方法
}
}
故對(duì)應(yīng)的java代碼為:
class JvmNameTest{
private static String name;
public static void setUserName(String name){
this.name = name;
}
public static String getName(){
return name;
}
public static void getInfos(){}
}
- JvmMultifileClass
該注解指示編譯器生成一個(gè)多文件類凝果,其中包含在此文件中聲明的頂級(jí)功能和屬性作為其一部分淤堵,相應(yīng)的多文件類名稱由JvmName指定
@file:JvmName("Utils")
@file:JvmMultifileClass
package com.today07
var fieldA: String? = null
fun getA(): String {
return "this is A"
}
@file:JvmName("Utils")
@file:JvmMultifileClass
package com.today07
fun getB(): String {
return "this is B"
}
class Test{
public static void main(String[] args){
Utils.getFieldA();
Utils.getA();
Utils.getB();
}
}
故對(duì)應(yīng)的java代碼為:
class Utils{
private static String fieldA;
public static String getFieldA(){
return fieldA
}
public static void setFieldA(String fieldA){
this.fieldA = fieldA;
}
public static void getA(){}
public static void getB(){}
}
- JvmOverloads
告訴Kotlin編輯器為此函數(shù)生成替換參數(shù)值的重載
@file:JvmName("JvmOverloadTest")
package com.today07
@JvmOverloads
fun getInfo(name: String, age: Int = 10):String{
return "name:$name age:$age"
}
class Test{
public static void main(String[] args){
JvmOverloadTest.getInfo("aaaa");
JvmOverloadTest.getInfo("bbbb",20);
}
}
故對(duì)應(yīng)的java代碼為:
class JvmOverloadTest{
public static String getInfo(String name){
this(name,10)
}
public static String getInfo(String name,int age){
return "name:$name age:$age"
}
}
- JvmStatic
該注解只能在object或者Companion object中使用
對(duì)于函數(shù)使用該注解抚垃,Kotlin編譯器將生成另一個(gè)靜態(tài)方法
對(duì)于屬性使用該注解迎吵,Kotlin編輯將生成其他的setter和getter方法
這個(gè)注解的作用其實(shí)就是消除java調(diào)用Kotlin的companion object對(duì)象時(shí)不能直接調(diào)用其靜態(tài)方法和屬性的問(wèn)題氨距。
package com.today07
object ObjectTest {
val name:String? =null
@JvmStatic
var age:String? =null
@JvmField
var sex:Boolean? =null
fun getTest(){}
@JvmStatic
fun getTestB(){
}
}
class Test{
public static void main(String[] args){
ObjectTest.INSTANCE.getTest();
ObjectTest.getTestB();
ObjectTest.INSTANCE.getName();
ObjectTest.getAge();
ObjectTest.sex = false;
}
}