Kotlin中的“公开"和“公开"有什么区别? [英] What is the difference between 'open' and 'public' in Kotlin?
问题描述
我是Kotlin的新手,在open
和public
关键字之间感到困惑.谁能告诉我这些关键字之间的区别吗?
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'sfinal
: it allows others to inherit from this class. By default, all classes in Kotlin arefinal
, 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屋!