问题摘要 - 如何将其转换为Scala类?
问题 - 调用不同超级构造函数的多个构造函数
Java类 -
public class ClassConstExample extends BaseClassExample {
private String xyzProp;
private string inType = "def";
private String outType = "def";
private String flagSpecial = "none";
public ClassConstExample(final String file, final String header, final String inType,
final String outType, final String flag) {
super(file);
init(header, inType, outType, flag);
}
public ClassConstExample(final String file, final String header, final String inType,
final String outType, final String flag, final String mode) {
super(file, mode);
init(header, inType, outType, flag);
}
public ClassConstExample(final String file, final String header, final String flag){
super(file);
//some logic here that's irrelevant to this
...
this.xyxProp = getXYZ(header);
this.flagSpecial = getFlagSpecial(flag);
}
...
}
Run Code Online (Sandbox Code Playgroud)
我一直在尝试将这个类的构造函数转换为scala大约一天,我无法在如何处理以下问题上取得任何进展 - (多个构造函数在Scala中调用不同的基类构造函数).有人会介意帮我改变这门课吗?我读过一些地方说过用superScala 标准调用不可能做到这一点,那么我该如何做到这一点呢?
必须调用主构造函数,因此任何其他构造函数都必须调用主构造函数或另一个将调用主构造函数的构造函数。super 的构造函数在主构造函数中作为继承声明的一部分被调用。这意味着您只能调用一个超级构造函数。
class BaseClassExample(file: String, mode: String) {
def this(file: String) = this(file, "mode")
}
class ClassConstExample(file: String, header: String, inType: String, outType: String, flag: String, mode: String) extends BaseClassExample(file, mode) {
def this(file: String, header: String, inType: String, outType: String, flag: String) = this(file, header, inType, outType, flag, "mode")
def this(file: String, header: String, flag: String) = this(file, header, "inType", "outType", flag)
}
Run Code Online (Sandbox Code Playgroud)
init从每个构造函数中调用该方法(只需在主构造函数中调用,甚至直接在主体中调用)| 归档时间: |
|
| 查看次数: |
121 次 |
| 最近记录: |