Scala:是否可以覆盖默认的案例类构造函数?
只是想知道这是否可能。 我实际上想做的是在将参数之一存储为 val 之前检查并可能修改它。
或者,我可以使用重载并将默认构造函数设为私有。在这种情况下,我还想将伴生对象中的默认工厂构造函数设为私有,我该怎么做?
非常感谢。
Adam
编辑:好吧,我发现将默认构造函数设为私有也会使默认工厂构造函数成为私有,所以我有一个解决方案,但我仍然有兴趣知道默认构造函数是否可重写
Just wondering if this is possible.
What I would actually like to do is check and possibly modify one of the arguments before it is stored as a val.
Alternatively, I could use an overload and make the default constructor private. In which case I would also like to make private the default factory constructor in the companion object, how would I do that?
Many thanks.
Adam
edit: well i figured out that making the default constructor private also makes the default factory constructor private, so i have a solution, i'm still interested to know if the default constructor is overridable though
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法选择更改默认构造函数存储其参数的方式(例如,通过在将参数存储为
val
之前修改参数),但您可以选择在参数被存储时抛出异常。是错误的(这将在存储参数后发生)您还可以选择实现调用第一个构造函数的其他构造函数
,但这些构造函数不会获取工厂方法。
您可以通过将其添加到伴随对象中来轻松地自己创建工厂方法
任何其他比这更复杂的事情,您都必须放弃案例类并自己实现它的部分:
apply
,取消应用
、等于
和hashCode
。 Scala 编程讨论了如何执行所有这些操作,并为equals
和hashCode
提供了良好的公式。You do not have the option of changing the way the default constructor stores its parameters (e.g. by modifying the parameters before they are stored as
val
s) but you do have the option of throwing an exception if the parameters are wrong (this will occur after the parameters are stored)You also have the option of implementing additional constructors that call the first constructor
but those don't get factory methods.
You could easily create the factory method yourself by adding it to the companion object
Anything else more complicated than that, and you have to forgo the case class and implement it's parts on your own:
apply
,unapply
,equals
, andhashCode
. Programming in Scala talks about how to do all of these, giving good formulas forequals
andhashCode
.辅助 case 类构造函数的存在不会导致编译器在类的伴生类中生成其他工厂方法,因此您不会获得
CaseClaseName(«辅助构造函数参数列表»>) 的便利
创建它们。您必须使用new
关键字。最好将您在备用工厂方法中描述的逻辑放在伴随对象中,并坚持使用主构造函数。
The presence of secondary case class constructors don't cause the compiler to produce additional factory methods in the class's companion, so you won't get the convenience of
CaseClaseName(«secondary constructor parameter list»>)
for creating them. You'll have to use thenew
keyword.It's better to put the sort of logic you're describing in alternate factory methods in the companion object and stick to using the primary constructor.
您可以重载构造函数。这与 C++ 或 Java 中的相同。只需创建另一个构造函数即可。
或者您可以查看这篇 SO帖子。
You can overload constructors. It's the same as in C++ or Java. Simply make another constructor.
Or you can see this SO post.
您可以通过在伴生对象中编写自己的
apply
(对于工厂)和unapply
(对于模式匹配)方法,将常规类转换为伪案例类。或者,您可以简单地在案例类的伴随对象中编写一个命名工厂方法。You can turn a regular class into a pseudo-case-class by writing your own
apply
(for the factory) andunapply
(for the pattern match) methods in the companion object. Or, you could simply write a named factory method in the case class's companion object.