kotlin 内部类

kotlin 内部类

lucas Lv4

class 嵌套类

使用 class 会被反编译成 static final class

inner class 内部类

使用 inner class 会被反编译成 final class

结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class InnerClassDemo {

inner class InnerClass {
var hello = "Hello"

fun printHello() {
println(hello)
}
}

class Clazz {
var hello = "Hello"

fun printHello() {
println(hello)
}
}
}

反编译成 java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@Metadata(
// 省略
)
public final class InnerClassDemo {
@Metadata(
// 省略
)
public final class InnerClass {
@NotNull
private String hello = "Hello";

@NotNull
public final String getHello() {
return this.hello;
}

public final void setHello(@NotNull String var1) {
Intrinsics.checkNotNullParameter(var1, "<set-?>");
this.hello = var1;
}

public final void printHello() {
System.out.println(this.hello);
}
}

@Metadata(
// 省略
)
public static final class Clazz {
@NotNull
private String hello = "Hello";

@NotNull
public final String getHello() {
return this.hello;
}

public final void setHello(@NotNull String var1) {
Intrinsics.checkNotNullParameter(var1, "<set-?>");
this.hello = var1;
}

public final void printHello() {
System.out.println(this.hello);
}
}
}
  • Title: kotlin 内部类
  • Author: lucas
  • Created at : 2025-04-24 21:46:18
  • Updated at : 2025-04-24 22:17:12
  • Link: https://darkflamemasterdev.github.io/2025/04/24/kotlin-内部类/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
kotlin 内部类