可见性
Kotlin 的可见性与Java的可见性的映射关系如下表所示:
Kotlin中的声明 | Java中的声明 |
---|---|
private | private |
protected | protected |
internal | public |
public | public |
例如下面的Kotlin代码:
class ProgrammingBook {
private var isbn: String = "978-7-111-44250-9"
protected var author: String = "Cay"
public var name: String = "Core Java"
internal var pages: Int = 300
private fun findISBN(): String = "978-7-111-44250-9"
protected fun findAuthor(): String = "Cay"
public fun findName(): String = "Core Java"
internal fun findPages(): Int = 300
}
对应的Java的代码是:
public final class ProgrammingBook {
private String isbn = "978-7-111-44250-9";
@NotNull
private String author = "Cay";
@NotNull
private String name = "Core Java";
private int pages = 300;
@NotNull
protected final String getAuthor() {
return this.author;
}
protected final void setAuthor(@NotNull String var1) {
Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
this.author = var1;
}
@NotNull
public final String getName() {
return this.name;
}
public final void setName(@NotNull String var1) {
Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
this.name = var1;
}
public final int getPages$production_sources_for_module_chapter10_interoperability_main() {
return this.pages;
}
public final void setPages$production_sources_for_module_chapter10_interoperability_main(int var1) {
this.pages = var1;
}
private final String findISBN() {
return "978-7-111-44250-9";
}
@NotNull
protected final String findAuthor() {
return "Cay";
}
@NotNull
public final String findName() {
return "Core Java";
}
public final int findPages$production_sources_for_module_chapter10_interoperability_main() {
return 300;
}
}
我们可以看到Kotlin中的可见性跟Java中的基本相同。
当前内容版权归 JackChan1999 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 JackChan1999 .