Scala:是否可以覆盖默认的案例类构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2660975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala: is it possible to override default case class constructor?
提问by adam77
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.
只是想知道这是否可能。我真正想做的是在将其中一个参数存储为 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
编辑:好吧,我发现将默认构造函数设为私有也会使默认工厂构造函数变为私有,所以我有一个解决方案,但我仍然有兴趣知道默认构造函数是否可覆盖
采纳答案by Randall Schulz
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 the newkeyword.
次要 case 类构造函数的存在不会导致编译器在类的同伴中生成额外的工厂方法,因此您将无法方便地CaseClaseName(?secondary constructor parameter list?>)创建它们。您必须使用new关键字。
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.
最好将您描述的那种逻辑放在伴随对象中的替代工厂方法中,并坚持使用主构造函数。
回答by Ken Bloom
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 vals) but you do have the option of throwing an exception if the parameters are wrong (this will occur after the parameters are stored)
您无法更改默认构造函数存储其参数的方式(例如,在将参数存储为vals之前修改参数),但您可以选择在参数错误时抛出异常(这将发生在参数被存储)
case class Foo(x:Int){
if (x<0) throw SomeException;
}
You also have the option of implementing additional constructors that call the first constructor
您还可以选择实现调用第一个构造函数的其他构造函数
case class Foo(x:Int){
def this(x:Int,y:Int) = this(x+y)
}
but those don't get factory methods.
但那些没有获得工厂方法。
You could easily create the factory method yourself by adding it to the companion object
您可以通过将其添加到伴随对象轻松地自己创建工厂方法
object Foo{
def apply(x:Int,y:Int) = new Foo(x,y)
}
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, and hashCode. Programming in Scalatalks about how to do all of these, giving good formulas for equalsand hashCode.
还有什么比这更复杂,你不得不放弃的情况下类并实现你自己的它的部分:apply,unapply,equals,和hashCode。Scala 编程讨论了如何完成所有这些,并给出了equals和 的良好公式hashCode。
回答by wheaties
回答by wheaties
You can turn a regular class into a pseudo-case-class by writing your own apply(for the factory) and unapply(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.
您可以通过在伴生对象中编写自己的apply(对于工厂)和unapply(对于模式匹配)方法将常规类转换为伪案例类。或者,您可以简单地在案例类的伴随对象中编写一个命名的工厂方法。

