Scala:是否可以覆盖默认的案例类构造函数?

发布于 2024-08-29 06:34:36 字数 224 浏览 5 评论 0原文

只是想知道这是否可能。 我实际上想做的是在将参数之一存储为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趴在窗边数星星i 2024-09-05 06:34:37

您无法选择更改默认构造函数存储其参数的方式(例如,通过在将参数存储为 val 之前修改参数),但您可以选择在参数被存储时抛出异常。是错误的(这将在存储参数后发生)

case class Foo(x:Int){
    if (x<0) throw SomeException;
}

您还可以选择实现调用第一个构造函数的其他构造函数

case class Foo(x:Int){
     def this(x:Int,y:Int) = this(x+y)
}

,但这些构造函数不会获取工厂方法。

您可以通过将其添加到伴随对象中来轻松地自己创建工厂方法

object Foo{
     def apply(x:Int,y:Int) = new Foo(x,y)
}

任何其他比这更复杂的事情,您都必须放弃案例类并自己实现它的部分:apply取消应用等于hashCodeScala 编程讨论了如何执行所有这些操作,并为 equalshashCode 提供了良好的公式。

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)

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 Scala talks about how to do all of these, giving good formulas for equals and hashCode.

§普罗旺斯的薰衣草 2024-09-05 06:34:37

辅助 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 the new 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.

幽梦紫曦~ 2024-09-05 06:34:37

您可以重载构造函数。这与 C++ 或 Java 中的相同。只需创建另一个构造函数即可。

class Foo( _input:Int ){
    def this() = this( 0 )
}

或者您可以查看这篇 SO帖子。

You can overload constructors. It's the same as in C++ or Java. Simply make another constructor.

class Foo( _input:Int ){
    def this() = this( 0 )
}

Or you can see this SO post.

吻风 2024-09-05 06:34:37

您可以通过在伴生对象中编写自己的 apply (对于工厂)和 unapply (对于模式匹配)方法,将常规类转换为伪案例类。或者,您可以简单地在案例类的伴随对象中编写一个命名工厂方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文