kotlin学习 - Jave 与 Kotlin 注意事项

kotlin学习 - Jave 与 Kotlin 注意事项

java中使用kotlin的顶层方法

1
2
3
4
5
//Test.kt

fun echo(name: String) {
System.out.println(name)
}
1
2
3
4
5
6
7
8
//Test.java

class TestDemo {
public static void main(String[] args) {
//java 中使用 kotlin文件中的方法,默认写法是 xxKt.echo("hello")
TestKt.echo("hello");
}
}

java中使用kotlin的方法 ( 自定义 kt 文件类名 )

1
2
3
4
5
6
7
8
9
10
//Test.kt

//自定义编译后生成的文件名
@file:JvmName("TestDemoCode")
//编译后会强制性的把多个文件内容合并在一起
@file:JvmMultifileClass
package com.jetpack.compose.demo.test
fun echo(name: String) {
System.out.println(name)
}
1
2
3
4
5
6
7
8
9

//Test.java

class TestDemo {
public static void main(String[] args) {
TestDemoCode.echo("hello");
}
}

java文件 kotlin文件 中的 class 使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fun main() {
// TestDemoJave.java
testClass(TestDemoJave::class.java)
testKClass(TestDemoJave::class)
// TestDemoKotlin.java
testKotlinClass(TestDemoKotlin::class)
}

fun testClass(clazz: Class<TestDemoJave>) {
println(clazz.simpleName)
}

fun testKClass(clazz: KClass<TestDemoJave>) {
println(clazz.simpleName)
}

fun testKotlinClass(clazz: KClass<TestDemoKotlin>) {
println(clazz.simpleName)
}

Kotlin 没有静态变量与 静态方法

1
2
3
4
5
6
7
8
9
10
11
12
//TestDemoKotlin.kt

object TestDemoKotlin {
@JvmStatic
fun sayMessageStatic(msg: String) {
println(msg)
}

fun sayMessage(msg: String) {
println(msg)
}
}
1
2
3
4
5
6
7
8
9
10
11
//TestDemoJave.java

public class TestDemoJave {
public static void main(String[] args) {
//静态方法
TestDemoKotlin.sayMessageStatic("hello");

TestDemoKotlin.INSTANCE.sayMessage("hello");
}
}

函数嵌套

用途 :  在某些条件下触发递归的函数,不希望被外部函数访问到的函数
1
2
3
4
5
6
7
8
9
10
11
fun function() {
val str = "hello world"
//函数嵌套
fun say(count: Int = 10) {
println(str)
if (count > 0) {
say(count - 1)
}
}
say()
}

在Java中使用Kotlin中的扩展函数

1
2
3
4
5
//TestDemoKotlin.kt

fun String?.isNoEmpty(): Boolean {
return this != null && this.length > 0
}
1
2
3
4
5
6
//TestDemoJave.java

String str = "hello word";
//使用kotlin中的扩展函数 (第一个参数 必须是 扩展类的对象)
// xxKt.isNoEmpty(str)
TestDemoKotlinKt.isNoEmpty(str);

lambda 在 kotlin 中 最多只支持22个参数(Function22)

1
2
3
4
5
6
7
8
* Function23.java


package kotlin;

public interface Function23<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21, P22, P23, R> extends Function<R> {
R invoke(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10, P11 p11, P12 p12, P13 p13, P14 p14, P15 p15, P16 p16, P17 p17, P18 p18, P19 p19, P20 p20, P21 p21, P22 p22, P23 p23);
}
1
2
3
4
5
6
7
8
9

val lambdaA = { a: Int, b: Int, c: Int, d: Int, e: Int, f: Int, g: Int, h: Int,i: Int, j: Int, k: Int, l: Int, m: Int, n: Int, o: Int, p: Int, q: Int, r: Int, s: Int, t: Int, u: Int, v: Int, w: Int ->
println("zhang tao")
}

fun main(args: Array<String>) {
// print("hello")
lambdaA(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
}

inline 将内联函数的函数体复制到调用处实现内联 ( 内联可能导致生成的代码增加 )

1
inline fun <T> lock(lock: Lock, body: () -> T): T { …… }

noinline 标记不希望内联的函数参数

1
inline fun foo(inlined: () -> Unit, noinline notInlined: () -> Unit) { …… }

Kotlin 可见性修饰符

修饰符 同类 同模块 子类 其他
public(公开,默认)
internal(模块) - -
protected(受保护) - -
private(私有) - - -

Java 访问控制修饰符

修饰符 同类 同包 子类 不同包
public(公开)
protected(受保护) -
没有修饰符(默认) - -
private(私有) - - -

单例 例子

1
2
3
4
5
6
7
8
9
10
11
class Single private constructor() {
companion object {
fun get(): Single {
return Holder.instance
}
}

private object Holder {
val instance = Single()
}
}

kotlin 封闭类(枚举类)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sealed class SuperCommand {
object A : SuperCommand()
object B : SuperCommand()
object C : SuperCommand()
object D : SuperCommand()
class E(var name: String) : SuperCommand()
}

fun exec(superCommand: SuperCommand) =
when (superCommand) {
SuperCommand.A -> {
}
SuperCommand.B -> {
}
SuperCommand.C -> {
}
SuperCommand.D -> {
}
is SuperCommand.E -> {

}
}