加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala重载了构造函数和超级

发布时间:2020-12-16 18:04:09 所属栏目:安全 来源:网络整理
导读:我无法理解如何在 Java上开发类似于以下内容的Scala代码: 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
我无法理解如何在 Java上开发类似于以下内容的Scala代码:

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.帮助.

附:我正在尝试创建自己的基于wicket WebPage的BaseWebPage,这是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,无法绕过其中一种辅助形式的主要构造函数,即以下方法不起作用:

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

所有辅助构造函数表单必须调用主要表单.从language specification(5.3.1构造函数定义):

A class may have additional constructors besides the primary constructor. These
are de?ned by constructor de?nitions of the form def this(ps1)…(psn) = e.
Such a de?nition introduces an additional constructor for the enclosing class,with
parameters as given in the formal parameter lists ps1,…,psn,and whose evaluation
is de?ned 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

(强调我的).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读