Pergunta

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); }
}

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

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

Foi útil?

Solução

Do you mean something like:

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) {
}

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()
}

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

A class may have additional constructors besides the primary constructor. These are defined by constructor definitions of the form def this(ps1)...(psn) = e. Such a definition introduces an additional constructor for the enclosing class, with parameters as given in the formal parameter lists ps1, ..., psn, and whose evaluation is defined by the constructor expression e. The scope of each formal parameter is the subsequent parameter sections and the constructor expression e. A constructor expression is either a self constructor invocation this(args1)...(argsn) or a block which begins with a self constructor invocation

(emphasis mine).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top