Scala 中子类化而不重复构造函数参数的快捷方式?
假设我有一些这样的类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕你一个人在那里。构造函数不是继承的或多态和子类构造函数,虽然它们必须并且总是调用其直接超类的构造函数,但不会也不可能自动完成此操作,除非有一个零参数构造函数,这是通过提及来暗示的“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.
这是您要找的吗?
Is this what you are looking for?