Scala重载了构造函数和super

Scala重载了构造函数和super

本文介绍了Scala重载了构造函数和super的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白如何在Java上开发类似于以下内容的Scala代码:

I can't understand how to develop Scala code similar to the following on Java:

public abstract class A {
   protected A() { ... }
   protected A(int a) { ... }
}

public abstract class B {
   protected B() { super(); }
   protected B(int a) { super(a); }
}

public class C extends B {
   public C() { super(3); }
}

虽然很清楚如何开发C类,但我不知道如何以获得B.帮助。

while it's clear how to develop C class, I can't get how to receive B. Help, please.

PS我试图创建自己的派生自wicket WebPage的BaseWebPage,这是Java的一种常见做法

P.S. I'm trying to create my own BaseWebPage derived from wicket WebPage which is a common practice for Java

推荐答案

abstract class A protected (val slot: Int) {
    protected def this() = this(0)
}

abstract class B protected (value: Int) extends A(value) {
    protected def this() = this(0)
}

class C extends B(3) {
}

有, AFAIK,无法从辅助形式之一绕过主要构造函数,即以下操作无效:

There is, AFAIK, no way to bypass the primary constructor from one of the secondary forms, i.e., the following will not work:

abstract class B protected (value: Int) extends A(value) {
    protected def this() = super()
}

所有辅助构造函数窗体都必须调用主窗体。来自(5.3.1构造函数定义):

All secondary constructor forms must call the primary one. From the language specification (5.3.1 Constructor Definitions):

(重点是我的)。

这篇关于Scala重载了构造函数和super的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:56