在Scala中不重复构造函数参数的子类化的快捷方式? [英] Shortcut for subclassing in Scala without repeating constructor arguments?

查看:137
本文介绍了在Scala中不重复构造函数参数的子类化的快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的类:

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

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

View的大多数子类都不会使用不同的构造函数参数。我想要能够这样写:

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
}

有一些快捷方式来编写子类,以便您不必显式定义构造函数args并将它们传递给超类(如果我改变超类的签名,我不必重写所有的子类)

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)?

推荐答案

恐怕你自己在那里。构造函数不是继承的,也不是多态和子类构造函数,而它们必须并且总是调用它们的直接超类的构造函数,不能并且不能自动完成,除非有一个零参数构造函数,超类的名称在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.

这篇关于在Scala中不重复构造函数参数的子类化的快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆