@JvmName("XX")?? 使用此注解指定编译kotlin类的自定义名字,在java类里可以直接使用该名称。
@file:JvmName("Hero")
//java类里直接使用该名称
Hero.makeProclamation()
@JvmField :给kotlin里的属性添加@JvmField,可以直接在java类里面调用 直接访问该属性。
@JvmField
val spells = listOf("Magic Ms. L", "Lay on Hans")
companion object{
//@JvmField 让java类访问此伴生对象里的属性
@JvmField
val MAX_SPELL_COUNT = 10
}
Spellbook.MAX_SPELL_COUNT
@JvmOverloads :让java类可以调用kotlin类里面重载的函数,否则java调用该函数时会报错。
@JvmOverloads
fun handOverFood(leftHand: String = "berries", rightHand: String = "beef") {
println("Mmmm... you hand over some delicious $leftHand and $rightHand")
}
//Java要支持,koltin类Hero的函数handOverFood要重载
Hero.handOverFood("apple");
@JvmStatic :在Java 类里可以直接调用Kotlin 伴生对象里的函数
companion object{
//@JvmField 让java类访问此伴生对象里的属性
@JvmField
val MAX_SPELL_COUNT = 10
@JvmStatic
fun getSpellbookGreeting() = println("I am the Great Grimoire!")
}
//在Java类里直接调用Kotlin类Spellbook的伴生对象的属性和方法
Spellbook.MAX_SPELL_COUNT;
Spellbook.getSpellbookGreeting();
? 异常:
?kotlin 调java的异常
? val adversary = Jhava()
try {
adversary.extendHandInFriendship()
} catch (e: Exception) {
println("Begone, foul beast!")
}
//java类里的异常
public void extendHandInFriendship() throws IOException {
throw new IOException();
}
java调用Kotlin里的异常 :
Kotlin里的异常 需要使用@Throws(IOException::class)注解,才能让java调用此异常方法
//Kotlin里的异常 需要@Throws(IOException::class)注解
@Throws(IOException::class) fun acceptApology() {
throw IOException()
}
//Java类里调用Kotlin 的异常
try{
Hero.acceptApology();
}catch (IOException e){
System.out.println("Caught!");
}
public class Jhava {
private int hitPoints = 3232320;
public static void main(String[] args) {
System.out.println(Hero.makeProclamation());
Spellbook spellbook = new Spellbook();
for (String spell : spellbook.spells) {
System.out.println(spell);
}
//Java要支持,koltin类Hero的函数handOverFood要重载
Hero.handOverFood("apple");
//伴生对象
//Spellbook.Companion.getMAX_SPELL_COUNT();
System.out.println(Spellbook.MAX_SPELL_COUNT);
Spellbook.getSpellbookGreeting();
try {
//强制在编译期进行处理
new Jhava().extendHandInFriendship();
} catch (IOException e) {
e.printStackTrace();
}
try{
Hero.acceptApology();
}catch (IOException e){
System.out.println("Caught!");
}
}
// @NotNull 一定不为空
@NotNull
public String utterGreeting() {
return "HELLO";
}
// @Nullable 可能为空
@Nullable
public String determineFriendshipLevel() {
return null;
}
public int getHitPoints() {
System.out.println("-------getHitPoints--------");
return hitPoints;
}
public void setHitPoints(int hitPoints) {
this.hitPoints = hitPoints;
}
public void extendHandInFriendship() throws IOException {
throw new IOException();
}
}
@file:JvmName("Hero")
import java.io.IOException
fun main() {
val adversary = Jhava()
println(adversary.utterGreeting())
//平台类型
val level = adversary.determineFriendshipLevel()
level?.toLowerCase()
adversary.hitPoints = 3
println(adversary.hitPoints)
handOverFood("apple")
//Spellbook.MAX_SPELL_COUNT
try {
adversary.extendHandInFriendship()
} catch (e: Exception) {
println("Begone, foul beast!")
}
}
fun makeProclamation() = "Greetings, beast!"
//调用者可以指定英雄左手或右手拿什么食物,或者使用默认的配置——左手拿浆果,右手拿牛肉。
//
@JvmOverloads
fun handOverFood(leftHand: String = "berries", rightHand: String = "beef") {
println("Mmmm... you hand over some delicious $leftHand and $rightHand")
}
@Throws(IOException::class)
fun acceptApology() {
throw IOException()
}
}
?
class Spellbook {
//@JvmField 让java类访问此属性
@JvmField
val spells = listOf("Magic Ms. L", "Lay on Hans")
companion object{
//@JvmField 让java类访问此伴生对象里的属性
@JvmField
val MAX_SPELL_COUNT = 10
@JvmStatic
fun getSpellbookGreeting() = println("I am the Great Grimoire!")
}
}
|