Scala 中子类化而不重复构造函数参数的快捷方式?

发布于 2024-08-09 01:54:49 字数 412 浏览 1 评论 0原文

假设我有一些这样的类:

abstract class View(val writer: XMLStreamWriter) {
    // Implementation
}

class TestView(writer: XMLStreamWriter) extends View(writer) {
    // Implementation
}

View 的大多数子类不会采用不同的构造函数参数。我希望能够写这样的东西:

class TestView extends View {
    // Implementation
}

是否有一些编写子类的快捷方式,以便您不必显式定义构造函数参数并将它们传递给超类(这样我就不必重新编写如果我更改超类的签名,我的所有子类)?

Let's say I have some classes like this:

abstract class View(val writer: XMLStreamWriter) {
    // Implementation
}

class TestView(writer: XMLStreamWriter) extends View(writer) {
    // Implementation
}

Most subclasses of View are not going to take different constructor arguments. I would like to be able to write something like this:

class TestView extends View {
    // Implementation
}

Is there some shortcut to write subclasses so that you don't have to explicitly define the constructor args and pass them to the superclass (so that I don't have to re-write all my subclasses if I change the signature of the superclass)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

中性美 2024-08-16 01:54:49

恐怕你一个人在那里。构造函数不是继承的或多态和子类构造函数,虽然它们必须并且总是调用其直接超类的构造函数,但不会也不可能自动完成此操作,除非有一个零参数构造函数,这是通过提及来暗示的“extends”子句中的超类名称。

I'm afraid you're on your own there. Constructors aren't inherited or polymorphic and subclass constructors, while they must and always do invoke a constructor for their immediate superclass, do not and cannot have that done automatically, except if there's a zero-arg constructor, which is implied by the mention of the superclass's name in an "extends" clause.

骑趴 2024-08-16 01:54:49
abstract class View {
    def writer: XMLStreamWriter
    // Implementation
}

class TestView(val writer: XMLStreamWriter) extends View {
    // Implementation
}

这是您要找的吗?

abstract class View {
    def writer: XMLStreamWriter
    // Implementation
}

class TestView(val writer: XMLStreamWriter) extends View {
    // Implementation
}

Is this what you are looking for?

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