Kotlin中的“公开"和“公开"有什么区别? [英] What is the difference between 'open' and 'public' in Kotlin?

查看:96
本文介绍了Kotlin中的“公开"和“公开"有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Kotlin的新手,在openpublic关键字之间感到困惑.谁能告诉我这些关键字之间的区别吗?

I am new to Kotlin and I am confused between open and public keywords. Could anyone please tell me the difference between those keywords?

推荐答案

open关键字的意思是开放扩展":

The open keyword means "open for extension":

上的open注释与Java的final相反:它允许其他人从此类继承.默认情况下,Kotlin中的所有类都是final,与有效的Java,第17项:设计和继承文档或禁止继承相对应.

The open annotation on a class is the opposite of Java's final: it allows others to inherit from this class. By default, all classes in Kotlin are final, which corresponds to Effective Java, Item 17: Design and document for inheritance or else prohibit it.

您还需要明确说明要使其可覆盖的方法,并用open标记:

You also need to be explicit about methods you want to make overridable, also marked with open:

open class Base {
    open fun v() {}
    fun nv() {}
}

public关键字用作可见性修饰符,可以应用于类,函数等. 请注意,如果未明确指定其他任何内容,则public是默认设置:

The public keyword acts as a visibility modifier that can be applied on classes, functions etc. Note that public is the default if nothing else is specified explicitly:

如果您未指定任何可见性修饰符,则默认使用public,这意味着您的声明将在任何地方都可见

If you do not specify any visibility modifier, public is used by default, which means that your declarations will be visible everywhere

这篇关于Kotlin中的“公开"和“公开"有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆