在科特林,空课的目的是什么? [英] What is the purpose of empty class in Kotlin?
问题描述
我正在浏览Kotlin
参考文档,然后我看到了. /p>
I was going through Kotlin
reference document and then I saw this.
类声明由类名称,类头组成 (指定其类型参数,主要构造函数等)以及 类身体,大括号包围.标头和正文 是可选的;如果班级没有肢体,则可以省略花括号.
The class declaration consists of the class name, the class header (specifying its type parameters, the primary constructor etc.) and the class body, surrounded by curly braces. Both the header and the body are optional; if the class has no body, curly braces can be omitted.
class Empty
现在我想知道没有标题和正文的此类声明的用途是什么
Now I'm wondering what is the use of such class declaration without header and body
推荐答案
tldr:他们想证明这是可能的
tldr: they want to demonstrate it's possible
即使是一个空类,其类型也是Any
,因此具有某些方法自动.我认为在大多数情况下,这没有意义,但是在文档情况下,它用于显示类的最简单定义.
even an empty class is of type Any
and therefore has certain methods automatically. I think in most cases, this does not make sense, but in the documentation case it's used to show the simplest possible definition of a class.
Java等效项为:
public final class Empty {
}
这篇关于在科特林,空课的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!