Scala 中的私有和受保护构造函数 [英] Private and protected constructor in Scala
本文介绍了Scala 中的私有和受保护构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很好奇在 Scala 中没有显式主构造函数的影响,只有类体的内容.
I've been curious about the impact of not having an explicit primary constructor in Scala, just the contents of the class body.
特别是,我怀疑私有或受保护的构造函数模式,即通过伴随对象或另一个类或对象的方法控制构造可能没有明显的实现.
In particular, I suspect that the private or protected constructor pattern, that is, controlling construction through the companion object or another class or object's methods might not have an obvious implementation.
我错了吗?如果是这样,它是如何完成的?
Am I wrong? If so, how is it done?
推荐答案
您可以通过在类名和参数列表之间插入适当的关键字来将默认构造函数声明为私有/受保护的,如下所示:
You can declare the default constructor as private/protected by inserting the appropriate keyword between the class name and the parameter list, like this:
class Foo private () {
/* class body goes here... */
}
这篇关于Scala 中的私有和受保护构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文